输入类型="提交"和按钮标签是可互换的吗?或者如果有任何区别,那么什么时候使用输入类型=“提交”和什么时候按钮?

如果没有区别,为什么我们有两个标签是为了同样的目的?


当前回答

如果你谈论的是<input type=button>,它不会自动提交表单

如果您谈论的是<button>标记,则该标记较新,不会在所有浏览器中自动提交。

底线,如果你想要表单在所有浏览器中点击提交,使用<input type="submit">

其他回答

当使用input type="submit"和button时,有一件事需要考虑。使用input type="submit",你不能在输入上使用CSS伪元素,但可以在按钮上使用!

这就是在样式化方面使用按钮元素而不是输入元素的原因之一。

<input type="button" />只是一个按钮,本身不会做任何事情。 <input type="submit" />,在表单元素中,当单击时将提交表单。

另一个有用的“特殊”按钮是<input type="reset" />,它将清除表单。

虽然这两个元素在功能上提供了相同的结果*,但我强烈建议您使用<button>:

Far more explicit and readable. input suggests that the control is editable, or can be edited by the user; button is far more explicit in terms of the purpose it serves Easier to style in CSS; as mentioned above, FIrefox and IE have quirks in which input[type="submit"] do not display correctly in some cases Predictable requests: IE has verying behaviours when values are submitted in the POST/GET request to the server Markup-friendly; you can nest items, for example, icons, inside the button. HTML5, forward-thinking; as developers, it is our responsibility to adopt to the new spec once it is officialized. HTML5, as of right now, has been official for over one year now, and has been shown in many cases to boost SEO.

*除了<button type="button">,默认情况下没有指定的行为。

总之,我强烈反对使用<input type="submit"/>。

我不知道这是一个bug还是一个功能,但我发现有非常重要的(至少在某些情况下)差异:<input type="submit">在请求中创建键值对,<button type="submit">没有。在Chrome和Safari中测试。

因此,当你的表单中有多个提交按钮,并且想知道哪个按钮被点击了时,不要使用按钮,而是使用input type="submit"。

http://www.w3.org/TR/html4/interact/forms.html#h-17.5

用BUTTON元素函数创建的按钮就像用INPUT元素创建的按钮一样,但是它们提供了更丰富的呈现可能性:BUTTON元素可以包含内容。例如,包含图像函数的BUTTON元素可能类似于类型设置为“image”的INPUT元素,但BUTTON元素类型允许内容。

所以就功能而言,它们是可互换的!

(别忘了,type="submit"是默认的with按钮,所以不要用它!)