3

我的表单中有两种验证方法。一个。jQuery 验证 B. Form.Submit() 处理程序中的一些复杂的硬编码规则。

一旦 a & b 验证都成功,我需要禁用提交按钮,这样用户就不会提交两次。

到目前为止,我最好的尝试涉及将“submitHandler”添加到 JQuery 验证中......

jQuery("#myForm").validate(
{
    submitHandler: function (form) {
        form.submit({
            success: function (submittedForm, action) {
                alert("submitting");
                $('input[type=submit]', submittedForm).attr('disabled', 'disabled');
            }
        });
    }
});     

当然,这似乎不起作用。当它到达 SubmitHandler 时,验证的两个阶段都已经运行了吗?如果 (a) JQuery 验证失败,则不会触发此事件。但是,如果 (b) 自定义验证失败,则尽管未提交表单,但仍会运行它。如果 (a & b) 都通过了,则运行它,但是永远不会到达内部的“成功”回调。所以我的输入永远不会被禁用。

这是 n 个提交验证中的 1 个的示例(它是一个动态表单,还有其他类似的验证,其中 .submit 用于在验证失败时返回 false)...

$('#myForm').submit(function () {
    // If one is checked they must both be checked.
    if ($(SessionRadioId_2).is(':checked') || $(SessionRadioId_4).is(':checked')) {
        if (!($(SessionRadioId_2).is(':checked')) || !($(SessionRadioId_4).is(':checked'))) {
            alert('If you plan on attending "ABC" you must select it in both Session 2 & 3.');
            return false;
        }
    }

谢谢,贾斯汀

4

1 回答 1

0

Since multiple submits is what you are trying to avoid, rather than disabling the submit button after validation, disable it before and then re-enable it if validation fails.

于 2011-09-16T17:49:56.807 回答