ASP.NET MVC - 如何prevent双击与jquery.validate.unobtrusive LIB提交? [英] ASP.NET MVC - How to prevent double click submit with jquery.validate.unobtrusive lib?

查看:185
本文介绍了ASP.NET MVC - 如何prevent双击与jquery.validate.unobtrusive LIB提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要避免双击提交行为。我使用的不显眼库中的客户端验证。我有以下的code避免双重CLIC:

I need to avoid the double click submitting behavior. I'm using the client validation with the unobtrusive library. I have the following code for avoiding the double clic:

jQuery.fn.preventDoubleSubmit = function () {
         var alreadySubmitted = false;
         return jQuery(this).submit(function () {

             if (alreadySubmitted)
                 return false;
             else {
                 alreadySubmitted = true;
             }
         });
     };

     jQuery('form').preventDoubleSubmit();

不幸的是,如果我的表格有一些validable字段(例如,必填字段)时,code以上仍然被解雇,所以,就算我改正表格上的任何失误,我将无法再次提交。

Unfortunately, if my form has some validable fields (for example, a required field), the code above is still being fired, hence, even if I correct any mistakes on the form, I won't be able to submit it again.

我怎么能火双击code后,验证已成功做了什么?

How can I fire the double click code after the validation has been succesfully done?

推荐答案

我用下面的code解决它:

I solved it with the following code:

var tryNumber = 0;
 jQuery('input[type=submit]').click(function (event) {
     var self = $(this);

     if (self.closest('form').valid()) {
         if (tryNumber > 0) {
             tryNumber++;
             alert('Your form has been already submited. wait please');
             return false;
         }
         else {
             tryNumber++;
         }
     };
 });

请注意:您也可以替换:

NOTE: You can also replace the:

return false;

行,为:

self.attr('disabled', true);

但是,如果你用你的提交按钮的名称,你的控制器额外的逻辑,他们将被送到空。 (你可以使用一个额外的隐藏字段提交之前,他们收取)

BUT, if you use the name of your submit buttons on your controller for extra logic, they will be sent as null. (you can use an additional hidden field to charge them before submitting)

这就是它,希望它有助于

that's it, hope it helps

罗德里戈

编辑:多亏了这些职位:
<一href=\"http://stackoverflow.com/questions/2581535/jquery-newbie-combine-validate-with-hidding-submit-button\">jquery新手:结合了hidding验证提交按钮

Thanks to these posts: jquery newbie: combine validate with hidding submit button.

这篇关于ASP.NET MVC - 如何prevent双击与jquery.validate.unobtrusive LIB提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆