输入类型=“提交” VS 按钮标签可以互换吗?
input type="submit"
和 button
标签可以互换吗?或者如果有任何区别,那么何时使用 input type="submit"
以及何时使用 button
?
如果没有区别,那么为什么我们有 2 个用于相同目的的标签?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
http://www.w3.org/TR/html4/interact /forms.html#h-17.5
因此,仅就功能而言,它们是可以互换的!
(请注意,
type="submit"
是button
的默认值)http://www.w3.org/TR/html4/interact/forms.html#h-17.5
So for functionality only they're interchangeable!
(Note,
type="submit"
is the default forbutton
)尽管这两个元素功能上提供相同的结果*,我强烈建议您使用
input
表明该控件可编辑,或者可以由用户编辑;button
就其用途而言更加明确 更input[type="submit"]
在某些情况下无法正确显示POST 中提交值时,IE 会出现非常奇怪的行为
/GET
向服务器发出*
总之,我强烈反对使用
。
Although both elements deliver functionally the same result *, I strongly recommend you use
<button>
: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 servesinput[type="submit"]
do not display correctly in some casesPOST
/GET
request to the server* With the exception of
<button type="button">
which by default has no specified behaviour.In summary, I highly discourage use of
<input type="submit"/>
.只是一个按钮,本身不会执行任何操作。
在表单元素内时,将在单击时提交表单。
另一个有用的“特殊”按钮是
,它将清除表单。
The
<input type="button" />
is just a button and won't do anything by itself.The
<input type="submit" />
, when inside a form element, will submit the form when clicked.Another useful 'special' button is the
<input type="reset" />
that will clear the form.使用<按钮>标签而不是 。这是 bootstrap 3 中建议的做法。
http://getbootstrap.com/css/#buttons-tags< /a>
Use <button> tag instead of <input type="button"..>. It is the advised practice in bootstrap 3.
http://getbootstrap.com/css/#buttons-tags
不支持其中的 HTML,因为它是单个自关闭标记。另一方面,
除非您有特殊原因,否则最好坚持使用
。
<input type='submit' />
doesn't support HTML inside of it, since it's a single self-closing tag.<button>
, on the other hand, supports HTML, images, etc. inside because it's a tag pair:<button><img src='myimage.gif' /></button>
.<button>
is also more flexible when it comes to CSS styling.The disadvantage of
<button>
is that it's not fully supported by older browsers. IE6/7, for example, don't display it correctly.Unless you have some specific reason, it's probably best to stick to
<input type='submit' />
.我意识到这是一个老问题,但我在 mozilla.org 上发现了这个问题,并认为它适用。
https://developer.mozilla.org/en- US/docs/Web/Guide/HTML/Forms/My_first_HTML_form#And_a_
I realize this is an old question but I found this on mozilla.org and think it applies.
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/My_first_HTML_form#And_a_<button>_to_finish
<button>
is newer than<input type="submit">
, is more semantic, easy to stylize and support HTML inside of it.虽然其他答案都很好并且回答了问题,但在使用
input type="submit"
和button
时需要考虑一件事。使用input type="submit"
,您不能在输入上使用 CSS 伪元素,但您可以获取一个按钮!这是在样式设置时在输入上使用
button
元素的原因之一。While the other answers are great and answer the question there is one thing to consider when using
input type="submit"
andbutton
. With aninput type="submit"
you cannot use a CSS pseudo element on the input but you can for a button!This is one reason to use a
button
element over an input when it comes to styling.我不知道这是一个错误还是一个功能,但我发现了一个非常重要的(至少在某些情况下)差异:
创建键值在您的请求中配对,而
因此,当您的表单中有多个提交按钮并且想知道单击了哪一个时 - 不要使用
button
,而是使用input type="submit"
。I don't know if this is a bug or a feature, but there is very important (for some cases at least) difference I found:
<input type="submit">
creates key value pair in your request and<button type="submit">
doesn't. Tested in Chrome and Safari.So when you have multiple submit buttons in your form and want to know which one was clicked - do not use
button
, useinput type="submit"
instead.它不会自动提交表单
如果您正在谈论
,如果您正在谈论
最重要的是,如果您希望在所有浏览器中单击时提交表单,请使用
If you are talking about
<input type=button>
, it won't automatically submit the formif you are talking about the
<button>
tag, that's newer and doesn't automatically submit in all browsers.Bottom line, if you want the form to submit on click in all browsers, use
<input type="submit">