jquery newbie: combine validate with hidding submit button

前端 未结 3 658
傲寒
傲寒 2021-01-07 09:48

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

      

        
相关标签:
3条回答
  • 2021-01-07 10:22

    I would do it like this:

    $('#PageForm').submit(function() {
       if($(this).valid()) $(':submit', this).attr('disabled', true);
    });
    

    This is triggered on submit of the form, looks for any submit button inside and disables it if the form was valid.

    Use $('#PageForm :submit').removeAttr('disabled'); to re-enable the button when ready.

    0 讨论(0)
  • 2021-01-07 10:25

    If you're using this validation plugin, it comes with a valid() method.

      jQuery('input[type=submit]').click(function() {
          var self = $(this);
    
          if (self.data('clicked')) {
              return false;
          } else if (self.closest('form').valid()) {
              self.data('clicked', true);
    
              return true;
          };
      });
    

    or even:

      jQuery('input[type=submit]').click(function(event) {
          var self = $(this);   
    
          if (self.closest('form').valid()) {
              self.attr('disabled', true);
          };
      });
    
    0 讨论(0)
  • 2021-01-07 10:27

    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();
        }
    });
    
    0 讨论(0)
提交回复
热议问题