ajaxForm插件在任何单击<按钮>上提交

ajaxForm plugin submits on any <button> clicked

本文关键字:按钮 提交 插件 任何 单击 ajaxForm      更新时间:2023-09-26

看看这个

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

我试图查看插件的来源并修复它,但它超出了我的范围。 captureSubmittingElement对我来说似乎很好。

有什么想法吗?

你应该

使用

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

按钮更改为键入按钮,默认为键入提交

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