2013-07-07 84 views
2

我有以下提交事件没有捕捉提交类型的输入

$(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; 
    }); 
}); 

的形式提到的Ajax GET请求是这样的

<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> 

在这种形式下,我有两个不同的提交按钮!各有不同的操作!

我跟踪的GET请求的URL是一样的东西下面

?input1=1&input2=2... 

我期待?input1=1&input2=2&submit=?input1=1&input2=2&preview=

为什么我不能在要求遵守“提交”或“预览”的名字??

+0

为什么不写类型''GET''而不是获取的方法?它会更短,并且还可以简化将来的错误。 –

回答

3

这是因为jQuery只有serializes the nodeTypes input, select, textarea and keygen,输入类型不是submit,button, image, file or reset

望着jQuery的源代码,你可以告诉表单元素输入类型对以下正则表达式检查:

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

此检查是在.serializeArray这是由.serialize在你的代码中调用。


有兴趣的读者 - 这是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