jQuery的新手:结合验证与隐藏提交按钮 [英] jquery newbie: combine validate with hidding submit button

查看:94
本文介绍了jQuery的新手:结合验证与隐藏提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的jQuery.我已经验证可以与此表单(MVC 1.0/C#)一起使用:

I'm new a jQuery. I have gotten validate to work with my form (MVC 1.0 / C#) with this:

      <script type="text/javascript">
      if (document.forms.length > 0) { document.forms[0].id = "PageForm"; document.forms[0].name = "PageForm"; }
      $(document).ready(function() {
          $("#PageForm").validate({
              rules: {
                  SigP: { required: true }
              },
              messages: {
                  SigP: "<font color='red'><b>A Sig Value is required. </b></font>"
              }
          });

      });
  </script>

我还想隐藏提交"按钮,以防止抽搐的鼠标综合症在控制器完成并重定向之前引起重复输入(我使用的是GPR模式).为此,可以使用以下方法:

I also want to hide the Submit button to prevent twitchy mouse syndrome from causing duplicate entry before the controller completes and redirects (I'm using an GPR pattern). The following works for this purpose:

  <script type="text/javascript">  
  //
  // prevent double-click on submit
  //
      jQuery('input[type=submit]').click(function() {
          if (jQuery.data(this, 'clicked')) {
              return false;
          }
          else {
              jQuery.data(this, 'clicked', true);
              return true;
          }
      });
  </script>

但是,我不能让两者一起工作.具体来说,如果单击提交"按钮后验证失败(根据表单的工作方式发生),那么除非我执行浏览器刷新以重置被单击"属性,否则无法再次提交表单.

However, I can't get the two to work together. Specifically, if validate fails after the Submit button is clicked (which happens given how the form works), then I can't get the form submitted again unless I do a browser refresh that resets the 'clicked' property.

如何重写上面的第二种方法以不设置单击属性,除非除非表单通过验证?

How can I rewrite the second method above to not set the clicked property unless the form validates?

谢谢.

推荐答案

对于那些仍在寻找像我一样的答案的人,这就是我的做法:

For those still looking for an answer like I was, this is how I did it:

$("#form1").validate({

    rules: {
        customer_title: "required",
        customer_firstName: "required",
        customer_lastName: "required"
        }
    },
    messages: {
        customer_title: "Please select a title",
        customer_firstName: "Please enter a firstname",
        customer_lastName: "Please enter a lastname"
        }
    },
        submitHandler: function(form) {
        //submitButtonX is the class attribute i placed on the button
        $('.submitButtonX').attr('disabled', 'disabled');
        form.submit();
    }
});

这篇关于jQuery的新手:结合验证与隐藏提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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