且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery表单验证和提交脚本冲突

更新时间:2023-09-26 11:58:10

jQuery似乎未阻止click.这很有道理;我确定该插件可能正在使用submit事件.

It looks like click is not prevented by jQuery validate. This makes sense; I'm sure the plugin is probably tapping into the submit event.

使用jQuery validate时,集成AJAX功能的***选择可能是在submitHandler回调中.因此,应该简单地移动一些代码即可:

When using jQuery validate, your best bet for integrating AJAX functionality is probably going to be in the submitHandler callback. So it should be a matter of simply moving some code around:

$("#nl").validate({
    errorContainer: "#errorcontainer",
    submitHandler: function () {
        $("#response").css("display", "none");
        $("#loading").css("display", "block");

        $.ajax({
            type: 'GET',
            url: 'http://app.icontact.com/icp/signup.php',
            data: $("form").serialize(),
            complete: function(results) {
                setTimeout(function() {
                    $("#loading").css("display", "none");
                    $("#response").css("display", "block");
                }, 1500);
            }
        });
    }
});

submitHandler回调中的代码替换了表单提交的默认操作(该操作将执行普通的HTTP请求).

The code in the submitHandler callback replaces the default action of the form submission (which is to do a vanilla HTTP request).