提交事件不捕获提交类型输入。

时间:2022-09-25 15:30:23

I have a Ajax GET request as mentioned below

我有一个Ajax GET请求,如下所示。

$(document).ready(function() {
    $('#comment-edit-form').submit(function() {
        $.ajax({
            type: $(this).attr('method'),
            url: '/comments/edit/' + $(this).attr('comment_pk') + '/',
            data: $(this).serialize(),
            success: function(data){}
        });
        return false;
    });
});

The form is something like this

形式是这样的。

<form method="get" id="comment-edit-form">
..

<input class="btn btn-primary" type="submit" name="preview" value="Preview">
<input class="btn btn-primary" type="submit" name="submit" value="Submit">

</form>

In this form, I have two different submit buttons! each has different operation!

在这个表单中,我有两个不同的提交按钮!每个人都有不同的操作!

The GET request URL I traced is something like below

我追踪的GET请求URL类似于下面。

?input1=1&input2=2...

I am expecting ?input1=1&input2=2&submit= or ?input1=1&input2=2&preview=

我的期望是?input1=1&input2=2&submit= or ?input1=1&input2=2&preview=。

Why I am not able to observe "submit" or "preview" names in the request??

为什么我不能在请求中看到“提交”或“预览”名称?

1 个解决方案

#1


3  

This is because jQuery only serializes the nodeTypes input, select, textarea and keygen with input types that are not submit,button, image, file or reset.

这是因为jQuery只将nodeTypes输入、选择、textarea和keygen序列化为不提交、按钮、图像、文件或重置的输入类型。

Looking at the jQuery source code, you can tell that form elements' input type is checked against the following regular expression:

查看jQuery源代码,可以看出表单元素的输入类型与以下正则表达式相对照:

 /^(?:submit|button|image|reset|file)$/i

This check is in .serializeArray which is called by .serialize in your code.

这个检查在. serializearray中,它是由.serialize在您的代码中调用的。


For the interested reader - this is the complete filter:

对于感兴趣的读者,这是完整的过滤器:

return this.name &&  // has a name
       !jQuery(this).is(":disabled") &&  // is not a disabled input
       rsubmittable.test(this.nodeName) && // is either input, select,textarea or keygen 
       !rsubmitterTypes.test(type) && // the test explained above
       (this.checked || !manipulation_rcheckableType.test(type)); // checked

#1


3  

This is because jQuery only serializes the nodeTypes input, select, textarea and keygen with input types that are not submit,button, image, file or reset.

这是因为jQuery只将nodeTypes输入、选择、textarea和keygen序列化为不提交、按钮、图像、文件或重置的输入类型。

Looking at the jQuery source code, you can tell that form elements' input type is checked against the following regular expression:

查看jQuery源代码,可以看出表单元素的输入类型与以下正则表达式相对照:

 /^(?:submit|button|image|reset|file)$/i

This check is in .serializeArray which is called by .serialize in your code.

这个检查在. serializearray中,它是由.serialize在您的代码中调用的。


For the interested reader - this is the complete filter:

对于感兴趣的读者,这是完整的过滤器:

return this.name &&  // has a name
       !jQuery(this).is(":disabled") &&  // is not a disabled input
       rsubmittable.test(this.nodeName) && // is either input, select,textarea or keygen 
       !rsubmitterTypes.test(type) && // the test explained above
       (this.checked || !manipulation_rcheckableType.test(type)); // checked