JQuery Validation Plugin submitHandler doesnt work for me

泪湿孤枕 提交于 2020-01-04 02:34:16

问题


Hello i got the following issue my following code doesn't work for me and i cant figure out why. I hope you guys can help me.

Edit: The submit handler simply doesn't get called. I don't even get the alert message. I wanted to disable the submit button till the form is validated.

    $('form').validate({

        onsubmit: true,
        debug: true,
        errorLabelContainer: "#errorBox",
        rules: {
            user_email: {
                minlength: 4,
                email: true,
                required: true
            },
            user_textField: {
                minlength: 3,
                required: true
            },
            user_phone: {
                required: true,
                number: true,
                minlength: 4
            },
            user_fax: {
                number: true,
                minlength: 4
            },

            user_plz: {
                required: true,
                number: true,
                minlength: 5
            }
        },
        submitHandler: function (form) {
            $('form').find(":submit").attr("disabled", true).attr("value","Submitting...");
            alert("hie");
            form.submit();
        }
    }); 

I also got a submit button:

<button class="btn btn-primary pull-right continuePaymentProcess" type="submit">Weiter</button>

Best Regards Grandy


回答1:


You don't need the onsubmit: true line because that's the default behavior. Important: as per docs,

Validate the form on submit. Set to false to use only other events for validation. Set to a Function to decide for yourself when to run validation. A boolean true is not a valid value.

In other words, you must remove that line because true will break the plugin.

As far as the submitHandler "not working", it works as per the documentation. The submitHandler callback function fires when the form is valid.

You say, "I wanted to disable the submit button till the form is validated."

Since the submitHandler doesn't get fired until the the form is valid, your code to disable the button doesn't fire until after the form is validated.

See your code: http://jsfiddle.net/aDAGL/




回答2:


I too faced the same issue. Please check all the input elements in the form and provide "name" attribute to all the elements . This helped in my case.

Thank you



来源:https://stackoverflow.com/questions/18354238/jquery-validation-plugin-submithandler-doesnt-work-for-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!