简体   繁体   English

jQuery ajax提交验证

[英]jQuery ajax submit validation

I'm trying to use the jQuery validation plugin and then submit my form via ajax, but I'm missing something. 我正在尝试使用jQuery验证插件,然后通过ajax提交我的表单,但是我缺少一些东西。 Anybody have any insight? 有人有见识吗?

$('#theForm').validate({
    focusInvalid: false,
    invalidHandler: function () {
        $('#message').addClass('error').html('Please fill out all necessary fields.').slideDown();
    },
    submitHandler: function (form) {
        $('#message').removeClass('error').html('Saving info.').slideDown();
        $.post(form.action, $(form).serialize(), function () {
            $('#message').removeClass('error').html('Saving complete.').slideUp(3500);
        });
    }
});

You're passing 你过去了

$(form).serialize() $(form).serialize()

to the second parameter of $.post which should be passed key/value pairs in the form of 到$ .post的第二个参数,该参数应以的形式传递键/值对

{ name: "John", time: "2pm" } {名称:“ John”,时间:“ 2pm”}

where as serialize() returns a query string. 其中,serialize()返回查询字符串。 Try using serializeArray() which returns a JSON data structure. 尝试使用serializeArray()返回JSON数据结构。 If that doesn't work you might need to manually retrieve each value from the form and pass them to $.post as key/value pairs. 如果这样不起作用,则可能需要从表单中手动检索每个值,并将它们作为键/值对传递给$ .post。

For submit form via AJAX, you should consider Malsup's Form Plugins . 要通过AJAX提交表单,您应该考虑使用Malsup的表单插件 It provide a better way to handle various form field's type. 它提供了更好的方式来处理各种表单字段的类型。 To make it work with jQuery validation plugins, see this example . 要使其与jQuery验证插件一起使用,请参见以下示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM