HTML按钮不提交表单 [英] 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>
这个按钮应该做的只是一些JavaScript。但即使它看起来就像上面的代码一样,它也会提交表单。当我将标签按钮更改为跨度时,它完美地起作用。但不幸的是,它需要是一个按钮。有没有办法阻止该按钮提交表单?像e。 g。
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 e. g.
<button onclick="document.getElementById('myform').doNotSubmit();">My Button</button>
推荐答案
我认为这是HTML最令人讨厌的小特性...该按钮需要是按钮类型才能不提交。
I think this is the most annoying little peculiarity of HTML... That button needs to be of type "button" in order to not submit.
<button type="button">My Button</button>
这篇关于HTML按钮不提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!