ajaxForm 插件在任何 <button> 被点击时提交

ajaxForm plugin submits on any <button> clicked

看看这个

http://jsbin.com/goyokir/edit?html,output

$(document).ready(function() { 
    $('#myForm').ajaxForm(function() { 
        alert("Form was submitted"); 
    }); 

  $('button').click(function(e){
    e.preventDefault(); //if removed, it acts as submit button
    $('body').append('regular clicked!<br>');
  });
}); 

我的问题是我的表单中有一些 <button>,当点击它们时出于某种奇怪的原因充当提交按钮。我可以使用 preventDefault 来解决这个问题,但另一个问题是,当您 关注输入并按 enter 它会触发它找到的第一个 <button>,而不是<input type='submit'>

我试图查看 plugin's source 并修复它,但它超出了我的范围。 captureSubmittingElement 对我来说似乎还好。

有什么想法吗?

将您的按钮更改为类型按钮,默认为类型提交

<button type="button">Regular &lt;Button&gt; 1</button>

你应该使用

<button type="button"></button>. 

此处演示:http://jsbin.com/xabuzukehe/1/edit?html,output

默认情况下,表单中的每个 button 作为提交按钮

使用 type="button" 防止 button 充当表单提交按钮。

<button type="button">Regular &lt;Button&gt; 1</button><br>
<button type="button">Regular &lt;Button&gt; 2</button><br>
<input type="submit" value="Input Submit button" />