以前,当我有一个只有一个提交按钮的表单时,我通常不会把 type="submit"
属性在那里。我从来没有观察到这会导致问题,并且一直认为“提交”是按钮的默认类型并且我可以依赖这种行为。
但是,w3schools 和 MDN 对我的说法是否正确存在分歧。
Tip: Always specify the type attribute for the
<button>
element. Different browsers may use different default types for the<button>
element.
submit
: ... This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
谁对谁错 - 无论是根据规范还是在真实浏览器中?
最佳答案
MDN 在其文章底部附近有一些规范链接,确认它所说的是正确的:button
元素的默认 type
是 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.reset:
Creates a reset button.button:
Creates a push button.
The
type
attribute controls the behavior of the button when it is activated. It is an enumerated attribute. The following table lists the keywords and states for the attribute — the keywords in the left column map to the states in the cell in the second column on the same row as the keyword.Keyword | State | Brief description --------+---------------+------------------ submit | Submit Button | Submits the form. reset | Reset Button | Resets the form. button | Button | Does nothing.
The missing value default is the Submit Button state.
W3Schools 的意思是,您应该始终指定该属性,以确保跨浏览器的行为尽可能一致。这并不意味着不能合法地省略该属性。
关于html - 我需要在提交按钮上指定类型 ="submit"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20610930/