HTML button to NOT submit form
I have a form. Outside that form, I have a button. A simple button, like this:
<button>My Button</button>
Nevertheless, when I click it, it submits the form. Here's the code:
<form id="myform">
<input />
</form>
<button>My Button</button>
All this button should do is some JavaScript. But even when it looks just like in the code above, it submits the form. When I change the tag button to span, it works perfectly. But unfortunately, it needs to be a button. Is there any way to block that button from submitting the form? Like eg
<button onclick="document.getElementById('myform').doNotSubmit();">My Button</button>
我认为这是HTML最令人讨厌的小特性...该按钮需要是“按钮”类型才能不提交。
<button type="button">My Button</button>
return false;
at the end of the onclick handler will do the job. However, it's be better to simply add type="button"
to the <button>
- that way it behaves properly even without any JavaScript.
Dave Markle is correct. From W3School's website:
Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".
In other words, the browser you're using is following W3C's specification.
链接地址: http://www.djcxy.com/p/36588.html上一篇: 如何防止按钮提交表单
下一篇: HTML按钮不提交表单