Why did my form start requiring a double click to submit after I added the jquery validation plug in? How do I change it?

前端 未结 1 2086
無奈伤痛
無奈伤痛 2020-12-04 03:29

I have a submit() event attached to my form submit button, before I started using the jquery validation plug in I could submit this form on a single click. Now for some reas

相关标签:
1条回答
  • 2020-12-04 04:12

    This is your whole problem...

    $("form").submit(function(){
    
        $(".selector").validate({
            ...
    

    You do not need to put .validate() inside of a submit event handler. The plugin already captures the submit event automatically. The .validate() method is the initialization method of the plugin on your form; it only gets attached to the form element and then only called once on DOM ready. (You presently need to double-click because it takes the first click just to initialize the plugin.)

    Just do this instead...

    $(document).ready(function() {
    
        ("#yourform").validate({
            // options & rules
        });
    
    });
    

    Simple DEMO: http://jsfiddle.net/x6Ckg/

    If, for whatever reason, you need to run other code on the submit event, you would use the submitHandler callback function provided by the plugin.

    $(document).ready(function() {
    
        ("#yourform").validate({
            //options & rules,
            submitHandler: function (form) {
                // stuff to do when form is valid
                // ajax, etc?
            }
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题