我们都知道如何在 HTML 中形成复选框输入:
<input name="checkbox_name" id="checkbox_id" type="checkbox">
我不知道的是——选中的复选框在技术上正确的值是多少?我已经看到这些都有效:
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked>
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="on">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="yes">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="checked">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="true">
答案是不重要吗?我没有看到任何证据表明答案被标记为正确here https://stackoverflow.com/questions/2874949/what-is-the-syntax-for-a-checked-checkbox-in-html来自spec http://www.w3.org/TR/html4/interact/forms.html itself:
复选框(和单选按钮)是可以切换的开/关开关
由用户。当控制元素被选中时,开关处于“打开”状态
属性已设置。提交表单时,仅“打开”复选框
控制才能成功。一个表单中的多个复选框可以共享
相同的控件名称。因此,例如,复选框允许用户
为同一属性选择多个值。使用 INPUT 元素
创建复选框控件。
规范编写者会说什么是正确答案?请提供基于证据的答案。
严格来说,你应该根据规范放置一些有意义的东西here https://www.w3.org/TR/html/infrastructure.html#boolean-attribute,最正确的版本是:
<input name=name id=id type=checkbox checked=checked>
对于 HTML,您还可以使用空属性语法 https://www.w3.org/TR/html/syntax.html#elements-attributes, checked=""
,或者甚至简单地checked
(对于更严格的 XHTML,这是不支持 http://www.w3.org/TR/xhtml1/guidelines.html#C_10).
然而,实际上,大多数浏览器都支持引号之间的任何值。将检查以下所有内容:
<input name=name id=id type=checkbox checked>
<input name=name id=id type=checkbox checked="">
<input name=name id=id type=checkbox checked="yes">
<input name=name id=id type=checkbox checked="blue">
<input name=name id=id type=checkbox checked="false">
并且只有以下内容将被取消选中:
<input name=name id=id type=checkbox>
另请参阅此类似问题disabled="disabled" https://stackoverflow.com/a/6531804/832136.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)