简体   繁体   English

具有jQuery'加载'图像功能的ASP.NET MVC客户端验证

[英]ASP.NET MVC client side validation with jQuery 'loading' image function

I have a jQuery function that shows a 'loading' image and disable the submit button when the form is submitted. 我有一个jQuery函数,可显示“正在加载”图像并在提交表单时禁用“提交”按钮。 I also added Html.EnableClientValidation(); 我还添加了Html.EnableClientValidation();

The problem is that when I click the submit button and the form is not valid then I get all the validation error messages but the 'loading' image never stops and the button stays 'disabled' 问题是,当我单击“提交”按钮并且表单无效时,我收到了所有验证错误消息,但“加载”图像从未停止,并且按钮保持“禁用”状态

I want to add functionality that stops the 'loading' image and enables the button when the form is not valid. 我想添加一些功能来停止“加载”图像并在表单无效时启用按钮。

I know how to write the jQuery function but I don't know how to integrate it with MVC EnableClientValidation. 我知道如何编写jQuery函数,但不知道如何将其与MVC EnableClientValidation集成。

I suppose that you are using an AJAX form because you are talking about disabling and enabling the submit button, In this case I would recommend you disabling the submit button just before sending the AJAX request which will ensure that client validation has passed. 我想您正在使用AJAX表单是因为您正在谈论禁用和启用提交按钮,在这种情况下,我建议您在发送AJAX请求之前禁用提交按钮,这将确保客户端验证已通过。

If you are using MS AJAX to AJAXify your form the two events you might try are OnBegin and OnComplete : 如果您使用MS AJAX对表单进行AJAX修饰,则可以尝试的两个事件是OnBeginOnComplete

<% Ajax.BeginForm(new AjaxOptions { 
    OnBegin = "enable" OnComplete = "disable" }) %>

If you are using jquery then you could subscribe for the beforeSend and complete events and this could be achieved globally with $.ajaxSetup . 如果使用的是jquery,则可以订阅beforeSendcomplete事件,这可以通过$ .ajaxSetup全局实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM