JQuery Validation插件submitHandler对我不起作用

时间:2022-06-28 20:35:44

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

最诚挚的问候格兰迪

2 个解决方案

#1


5  

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

您不需要onsubmit:true行,因为这是默认行为。重要提示:根据文档,

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.

在提交时验证表单。设置为false以仅使用其他事件进行验证。设置为函数以自行决定何时运行验证。布尔值true不是有效值。

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

换句话说,您必须删除该行,因为true将破坏插件。

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

至于submitHandler“不工作”,它按照文档工作。当表单有效时,将触发submitHandler回调函数。

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.

由于在表单有效之前不会触发submitHandler,因此在验证表单之后才会触发禁用该按钮的代码。

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

查看您的代码:http://jsfiddle.net/aDAGL/

#2


0  

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.

我也遇到了同样的问题。请检查表单中的所有输入元素,并为所有元素提供“name”属性。这有助于我的情况。

Thank you

#1


5  

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

您不需要onsubmit:true行,因为这是默认行为。重要提示:根据文档,

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.

在提交时验证表单。设置为false以仅使用其他事件进行验证。设置为函数以自行决定何时运行验证。布尔值true不是有效值。

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

换句话说,您必须删除该行,因为true将破坏插件。

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

至于submitHandler“不工作”,它按照文档工作。当表单有效时,将触发submitHandler回调函数。

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.

由于在表单有效之前不会触发submitHandler,因此在验证表单之后才会触发禁用该按钮的代码。

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

查看您的代码:http://jsfiddle.net/aDAGL/

#2


0  

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.

我也遇到了同样的问题。请检查表单中的所有输入元素,并为所有元素提供“name”属性。这有助于我的情况。

Thank you