【发布时间】:2014-09-12 13:28:41
【问题描述】:
当不显眼的验证返回错误时,我试图在我的 MVC 视图上运行一个方法。我当前的脚本根本不会触发。我试图只在返回错误时运行某个方法。
我尝试过$("#submitButton").click 和类似的方法,但无济于事。
是否只能从内联脚本执行此操作?验证是否有任何触发器?
<fieldset class="step">
<legend>
Contact:
</legend>
<p>Let us know how we can contact you with your quote, or anything else we may need from you coming up to your ski trip. </p>
<div style=" float: left;">
<table>
<tr style="height: 50px; padding: 5px;">
<td style="width: 187px;">
Full Name*
</td>
<td>
@Html.DropDownListFor(x => x.Title, Model.Titles, "...........", new { style = "width:70px; padding: 5px;" })
@Html.TextBoxFor(x => x.FirstName, new { style = "width:115px; padding: 5px;" })
@Html.TextBoxFor(x => x.LastName, new { style = "width:115px; padding: 5px;" })
</td>
</tr>
<tr style="height: 50px; padding: 5px;">
<td style="width: 187px;">
City*
</td>
<td>
@Html.TextBoxFor(x => x.City, new { style = "width:320px; padding: 5px;" })
</td>
</tr>
<tr style="height: 50px; padding: 5px;">
<td style="width: 187px;">
Postcode*
</td>
<td>
@Html.TextBoxFor(x => x.Postcode, new { style = "width:320px; padding: 5px;" })
</td>
</tr>
<tr style="height: 50px; padding: 5px;">
<td style="width: 187px;">
Email*
</td>
<td>
@Html.TextBoxFor(x => x.Email, new { style = "width:320px; padding: 5px;" })
</td>
</tr>
<tr style="height: 50px; padding: 5px;">
<td style="width: 187px;">
Telephone*
</td>
<td>
@Html.TextBoxFor(x => x.Telephone, new { style = "width:320px; padding: 5px;" })
</td>
</tr>
<tr style="height: 50px; padding: 5px;">
<td style="width: 187px;">
Preferred Time of Contact
</td>
<td>
@Html.TextBoxFor(x => x.PreferredTime, new { style = "width:200px; padding: 5px;" })
</td>
</tr>
</table>
</div>
<input id="submitButton" type="image" alt="Submit" src="/images/global/submit_button.gif" />
</fieldset>
编辑:刚刚尝试绑定到 invalid-form.validate 方法,但没有成功:
$(function () {
$("form").bind("invalid-form.validate", function () {
alert('invalid!');
});
});
【问题讨论】:
-
返回错误是什么意思?你看到了什么,会发生什么?
-
如果验证失败,那么我希望能够运行一个方法
-
您可以像这样从 jquery 调用检查: var controls = $("#divid").find('.class'); $.each(controls, function (index, item) { $("#form1").validate().element($(this)); if ($(this).valid) { //可能跳过 } else { //做一些事情,如果需要的话,甚至是一个 ajax 调用 }
-
点击提交按钮已经触发了验证。当验证失败时,我需要添加额外的功能来集中注意力。我不能使用 onclick,因为页面不允许我提交表单,因为验证,所以我不能运行 onclick
标签: jquery asp.net-mvc unobtrusive-validation