html - 我需要在提交按钮上指定类型 ="submit"吗?

标签 html forms browser specifications

以前,当我有一个只有一个提交按钮的表单时,我通常不会把 type="submit"属性在那里。我从来没有观察到这会导致问题,并且一直认为“提交”是按钮的默认类型并且我可以依赖这种行为。

但是,w3schools 和 MDN 对我的说法是否正确存在分歧。

w3schools claims:

Tip: Always specify the type attribute for the <button> element. Different browsers may use different default types for the <button> element.

鉴于MDN claims:

  • 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 元素的默认 typesubmit.

W3C HTML 4.01 :

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.

W3C HTML5 :

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/

相关文章:

html - 如何将溢出的跨度包装在固定的 div 中

django - 如何像在 django 管理站点中那样添加外键字段?

javascript - 将 angularjs 指令添加到 native html 元素(输入文本、div、span 等)?

html - Bootstrap 卡片不像示例中那样显示

javascript - 如何验证复选框中具有多个名称的复选框(使用引导验证器)

javascript - 如何在 JavaScript 中检测耳机是否插入或移除?

javascript - Jquery检测滚动条是否在顶部

css - 什么软件可以作为 CSS 的多合一跨浏览器?

html - 如何在 ipad 中使 div 可滚动?

javascript - 如何在克隆元素上应用事件?