[英]ASP.NET MVC: Disable Submit button if Client Validation passes
I have a simple MVC4 form with Client Validation enabled: 我有一个启用了客户端验证的简单MVC4表单:
@{ Html.EnableClientValidation(); }
@{ Html.EnableUnobtrusiveJavaScript(); }
@using (Html.BeginForm("Search", "Search", FormMethod.Post, new { id = "frmMain" }))
{
@Html.AntiForgeryToken() .....
<input type="submit" value="Search" id="btnSubmit" />
When the user clicks the submit and all validation passes on the client , I wish to have the submit button disabled as per jQuery below. 当用户单击客户端上的提交和所有验证通过时 ,我希望按照下面的jQuery禁用提交按钮。 How do I know if the validation has passed client side in side my jQuery script?
我怎么知道验证是否已通过我的jQuery脚本中的客户端?
<script type="text/javascript">
$(document).ready(function () {
$('input[type=submit]').click(function (e) {
$(this).attr('disabled', true);
$(this).attr('value', 'Working');
$('#frmMain').submit();
});
});
You can hook this code into the form validator. 您可以将此代码挂钩到表单验证器中。 Just make sure it executes AFTER the jquery.validator.unobtrusive.
只需确保它在jquery.validator.unobtrusive之后执行。
<script>
$("#you-form-id").data("validator").settings.submitHandler = function(form) {
$("#submit-button").attr("disabled",true).val("Working");
form.submit();
};
</script>
I had some issues when return a partail from the controller action. 从控制器操作返回partail时我遇到了一些问题。 Instead I hooked in to the submit function and checked that the form was valid like this:
相反,我挂钩到提交函数并检查表单是否有效如下:
$('form').submit(function () {
if ($(this).valid()) {
$('#order-form-submit-button').attr("disabled", true).val("wait...");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.