javascript <按钮>标签点击时的标准行为是什么?它会提交表格吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4667979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the standard behavior when < button > tag click? will it submit the form?
提问by lovespring
The tag <button>, not <input type=button>.
标签<button>,不是<input type=button>。
回答by raRaRa
If the button is within a form, the default behavior is submit.
如果按钮在表单内,则默认行为是提交。
If the button is not within a form, it will do nothing.
如果按钮不在表单中,它什么也不做。
BUT BE AWARE!
但请注意!
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".
始终为按钮指定类型属性。Internet Explorer 的默认类型是“按钮”,而在其他浏览器(以及 W3C 规范中)是“提交”。
Taken from http://www.w3schools.com/tags/tag_button.asp
回答by Ivo Wetzel
Yes it default to the submittype.
是的,它默认为submit类型。
type = submit|button|reset [CI]
This attribute declares the type of the button. Possible values:
submit: Creates a submit button. This is the default value.
type = submit|button|reset [CI]
这个属性声明了按钮的类型。可能的值:
submit: 创建一个提交按钮。这是默认值。
See: http://www.w3.org/TR/html401/interact/forms.html#h-17.5
请参阅:http: //www.w3.org/TR/html401/interact/forms.html#h-17.5
So when the buttonis inside a form it will submit it, when it's not inside a form, it still defaults to submitbut does nothing (since there's no form associated with it).
因此button,当它位于表单内时,它将提交它,当它不在表单内时,它仍然默认为submit但不执行任何操作(因为没有与之关联的表单)。
As raRaRa has pointed out below older versions of IE have the buttontag default type set to button: http://www.thefutureoftheweb.com/blog/button-wont-submit-in-ie
正如 raRaRa 在下面指出的旧版本 IE 将button标签默认类型设置为button:http: //www.thefutureoftheweb.com/blog/button-wont-submit-in-ie

