checked 和 selected 的哪些值是 false?
我认为根据 W3 规范,你应该这样做
<input type="checkbox" checked="checked" />
,
selected="selected"
但是,大多数浏览器会接受你只写 "CHECKED"
并且不给它一个值。那么,如果您确实包含该属性,是否有任何值(始终)被视为 false?
I think according to W3 spec, you're supposed to do
<input type="checkbox" checked="checked" />
And
selected="selected"
But, most browsers will accept it you just write "CHECKED"
and don't give it a value. So, what if you do include the attribute, are there any values that would be (consistently) considered false?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有任何值会导致复选框被取消选中。如果
checked
属性存在,则无论您将其设置为什么值,都会选中该复选框。渲染所有现代浏览器(FF3.6、Chrome 10、IE8)中检查的所有内容。
There are no values that will cause the checkbox to be unchecked. If the
checked
attribute exists, the checkbox will be checked regardless of what value you set it to.Renders everything checked in all modern browsers (FF3.6, Chrome 10, IE8).
checked
和selected
属性只允许有两个值,即属性名称的副本和(从 HTML 5 开始)空字符串。给出任何其他值都是错误的。如果您不想设置该属性,则必须省略整个属性。
请注意,在HTML 4 您可以省略除值之外的所有内容。 HTML 5 对此进行了更改,以忽略除名称之外的所有内容(这使得没有实际差异)。
因此,完整的(除了 cAsE 中的变化)属性的有效表示集是:
作为 text/html(HTML 或 XHTML)的文档将通过标签汤解析器进行馈送,并且检查属性的存在(与任何value) 将被视为“应检查此元素”。因此,当无效时,
checked="true"
、checked="yes"
和checked="false"
都会触发选中状态。如果为属性赋予不同的值,我没有任何兴趣去了解 XML 解析模式有哪些错误恢复机制,但我期望 HTML 和/或简单的错误恢复将以相同的方式处理它:如果该属性存在,则检查该元素。
(以上所有内容同样适用于
selected
和checked
。)The
checked
andselected
attributes are allowed only two values, which are a copy of the attribute name and (from HTML 5 onwards) an empty string. Giving any other value is an error.If you don't want to set the attribute, then the entire attribute must be omitted.
Note that in HTML 4 you may omit everything except the value. HTML 5 changed this to omit everything except the name (which makes no practical difference).
Thus, the complete (aside from variations in cAsE) set of valid representations of the attribute are:
Documents served as text/html (HTML or XHTML) will be fed through a tag soup parser, and the presence of a checked attribute (with any value) will be treated as "This element should be checked". Thus, while invalid,
checked="true"
,checked="yes"
, andchecked="false"
will all trigger the checked state.I've not had any inclination to find out what error recovery mechanisms are in place for XML parsing mode should a different value be given to the attribute, but I would expect that the legacy of HTML and/or simple error recovery would treat it in the same way: If the attribute is there then the element is checked.
(And all the above applies equally to
selected
as it does tochecked
.)没有任何值被视为错误,只有属性不存在才被视为错误。不过,存在大量无效值,并且某些实现可能会将某些无效值视为 false。
HTML5 规范
http://www. w3.org/TR/html5/forms.html#attr-input-checked :
http://www.w3.org/TR/html5/infrastruct.html #boolean-attributes:
结论
以下是有效、等价和正确:
以下是无效:
缺少属性是唯一有效的语法false:
建议
如果您关心编写有效的 XHTML,请使用
checked="checked"
,因为
是无效和其他替代方案的可读性较差。否则,只需使用
因为它更短。No value is considered false, only the absence of the attribute. There are plenty of invalid values though, and some implementations might consider certain invalid values as false.
HTML5 spec
http://www.w3.org/TR/html5/forms.html#attr-input-checked :
http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :
Conclusion
The following are valid, equivalent and true:
The following are invalid:
The absence of the attribute is the only valid syntax for false:
Recommendation
If you care about writing valid XHTML, use
checked="checked"
, since<input checked>
is invalid and other alternatives are less readable. Else, just use<input checked>
as it is shorter.实际上,HTML 4.01 规范规定这些属性不需要值。我个人还没有遇到过提供值会使这些控件呈现为未选中状态的情况。
以下是 selected 的规范文档的相应链接并已检查。
编辑: Firebug 会将复选框呈现为选中状态,无论我在选中属性的引号中输入任何值(包括仅键入“checked”而没有任何值),并且 IE 8 的开发人员工具强制 check="checked ”。然而,我不确定其他浏览器是否存在任何类似的工具可能会改变复选框的渲染状态。
Actually, the HTML 4.01 spec says that these attributes do not require values. I haven't personally encountered a situation where providing a value rendered these controls as unselected.
Here are the respective links to the spec document for selected and checked.
Edit: Firebug renders the checkbox as checked regardless of any values I put in quotes for the checked attribute (including just typing "checked" with no values whatsoever), and IE 8's Developer Tools forces checked="checked". I'm not sure if any similar tools exist for other browsers that might alter the rendered state of a checkbox, however.
空字符串通常为 false。显然,空字符串在所有浏览器中都不会被视为空,并且检查属性的存在意味着已检查。因此整个属性必须存在或省略。
The empty string is false as a rule.Apparently the empty string is not respected as empty in all browsers and the presence of the checked attribute is taken to mean checked. So the entire attribute must either be present or omitted.
checked 属性可以在 javascript 中通过以下方式设置。
(isOrdered 存储一个布尔值)
checked attribute can be set in the following way in javascript.
(isOrdered stores a boolean value)