使用jQuery验证上传的形式,在submitHandler(使用AJAX)...不工作形式、上传、工作、jQuery

2023-09-10 21:38:44 作者:迁就°

我正在使用jQuery的验证与表单。我想用ajax提交表单。当我把Ajax调用在验证的submitHandler()浏览器挂起。这是怎么回事?

I am using jquery validate with a form. I want to submit the form using ajax. When I put the ajax call in validate's submitHandler() The browser hangs. What's going on?

我得到的错误消息时启用验证方法的调试是:

The error message I get when I enable the validate method's debug is:

未捕获的异常:[异常...   在WrappedNative非法操作   原型对象nsresult:   0x8057000c   (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)   位置:JS框架::    http://ajax.googleapis.com/ajax/libs /jquery/1.4.2/jquery.min.js   ::˚F::行132的数据:无]

uncaught exception: [Exception... "Illegal operation on WrappedNative prototype object" nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame :: http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js :: f :: line 132" data: no]

这是太神秘,我做的感觉。

Which is too arcane for me to make sense of.

有些code:

$(document).ready(function() {
$("#loginForm").validate({
        debug: true,
        errorLabelContainer: $('div.error'),
        wrapper: 'li',
        rules:  {
            last_name: {
                required: true
            }
        },
        messages: {
            last_name: {
                required: "Please enter your last name."
            }
        },
        submitHandler: function(form){
            $.ajax({
                type: "POST",
                url: "test.php",
                data: form,
                success: function(msg){
                    console.log( "Data Saved: " + msg );
                },
                error: function(msg){
                    console.log( "Error: " + msg);
                }
            });
        }
    });

});

的形式是一个非常香草形式。通过标准的POST提交工作正常。此外,确认工作正常......它只是失败我提交与阿贾克斯的部分。

The form is a very vanilla form. Submitting via a standard POST works fine. Also, the validation works fine... it's just the submitting with ajax part that fails me.

推荐答案

您可能会想在发送前以连载的形式 - 你可能不希望发出一个DOM对象

You'll probably want to serialise that form before sending it - you probably don't want to send a DOM object

编辑RE:发表评论 - 只选择一些投入和序列化 -

edit RE:comment - to select only some inputs and serialize -

$(form).find(":input[name=inp1] :input[name=inp2]").serialize()