简体   繁体   English

ajaxForm插件在任何<button>点击后</button>提交

[英]ajaxForm plugin submits on any <button> clicked

Take a look at this 看看这个

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

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

My problem is that my form has some <button> 's in it and when clicked they act as submit buttons for some strange reason. 我的问题是我的表单中有一些<button> ,当单击它们时,由于某种奇怪的原因,它们充当了提交按钮。 I can use preventDefault to fix that but the other problem is that when your focus on an input and press enter it triggers the first <button> it finds, not the <input type='submit'> 我可以使用preventDefault来解决此问题,但另一个问题是,当您专注于输入并按Enter时,它会触发它找到的第一个<button> ,而不是<input type='submit'>

I tried to look at the plugin's source and fix it but it's beyond me. 我试图查看插件的源代码并进行修复,但这超出了我的范围。 captureSubmittingElement seems to be just fine to me. captureSubmittingElement对我来说似乎还不错。

Any ideas? 有任何想法吗?

You should use 你应该用

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

Demo'd here: http://jsbin.com/xabuzukehe/1/edit?html,output 在此处进行演示: http ://jsbin.com/xabuzukehe/1/edit?html,输出

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

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

Every button inside a form acts as a submit button by default 默认情况下,表单中的每个button充当提交按钮

Use type="button" to prevent a button from acting as a form submission 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" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM