checked 和 selected 的哪些值是 false?

发布于 2024-10-03 14:27:09 字数 245 浏览 5 评论 0原文

我认为根据 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

2024-10-10 14:27:09

没有任何值会导致复选框被取消选中。如果 checked 属性存在,则无论您将其设置为什么值,都会选中该复选框。

<input type="checkbox" checked />
<input type="checkbox" checked="" />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="unchecked" />
<input type="checkbox" checked="true" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="on" />
<input type="checkbox" checked="off" />
<input type="checkbox" checked="1" />
<input type="checkbox" checked="0" />
<input type="checkbox" checked="yes" />
<input type="checkbox" checked="no" />
<input type="checkbox" checked="y" />
<input type="checkbox" checked="n" />

渲染所有现代浏览器(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.

<input type="checkbox" checked />
<input type="checkbox" checked="" />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="unchecked" />
<input type="checkbox" checked="true" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="on" />
<input type="checkbox" checked="off" />
<input type="checkbox" checked="1" />
<input type="checkbox" checked="0" />
<input type="checkbox" checked="yes" />
<input type="checkbox" checked="no" />
<input type="checkbox" checked="y" />
<input type="checkbox" checked="n" />

Renders everything checked in all modern browsers (FF3.6, Chrome 10, IE8).

如果没有 2024-10-10 14:27:09

checkedselected 属性只允许有两个值,即属性名称的副本和(从 HTML 5 开始)空字符串。给出任何其他值都是错误的。

如果您不想设置该属性,则必须省略整个属性。

请注意,在HTML 4 您可以省略除值之外的所有内容。 HTML 5 对此进行了更改,以忽略除名称之外的所有内容(这使得没有实际差异)。

因此,完整的(除了 cAsE 中的变化)属性的有效表示集是:

<input ... checked="checked"> <!-- All versions of HTML / XHTML -->
<input ...          checked > <!-- Only HTML 4.01 and earlier -->
<input ... checked          > <!-- Only HTML 5 and later -->
<input ... checked=""       > <!-- Only HTML 5 and later -->

作为 text/html(HTML 或 XHTML)的文档将通过标签汤解析器进行馈送,并且检查属性的存在(与任何value) 将被视为“应检查此元素”。因此,当无效时, checked="true"checked="yes"checked="false" 都会触发选中状态。

如果为属性赋予不同的值,我没有任何兴趣去了解 XML 解析模式有哪些错误恢复机制,但我期望 HTML 和/或简单的错误恢复将以相同的方式处理它:如果该属性存在,则检查该元素。

(以上所有内容同样适用于 selectedchecked。)

The checked and selected 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:

<input ... checked="checked"> <!-- All versions of HTML / XHTML -->
<input ...          checked > <!-- Only HTML 4.01 and earlier -->
<input ... checked          > <!-- Only HTML 5 and later -->
<input ... checked=""       > <!-- Only HTML 5 and later -->

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", and checked="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 to checked.)

千と千尋 2024-10-10 14:27:09

没有任何值被视为错误,只有属性不存在才被视为错误。不过,存在大量无效值,并且某些实现可能会将某些无效值视为 false。

HTML5 规范

http://www. w3.org/TR/html5/forms.html#attr-input-checked

禁用的内容属性是一个布尔属性。

http://www.w3.org/TR/html5/infrastruct.html #boolean-attributes

元素上存在布尔属性表示真值,不存在该属性表示假值。

如果该属性存在,则其值必须是空字符串或与该属性的规范名称匹配的 ASCII 不区分大小写的值,并且没有前导或尾随空格。

结论

以下是有效、等价和正确

<input type="checkbox" checked />
<input type="checkbox" checked="" />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="ChEcKeD" />

以下是无效

<input type="checkbox" checked="0" />
<input type="checkbox" checked="1" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="true" />

缺少属性是唯一有效的语法false

<input type="checkbox" />

建议

如果您关心编写有效的 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 :

The disabled content attribute is a boolean attribute.

http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

Conclusion

The following are valid, equivalent and true:

<input type="checkbox" checked />
<input type="checkbox" checked="" />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="ChEcKeD" />

The following are invalid:

<input type="checkbox" checked="0" />
<input type="checkbox" checked="1" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="true" />

The absence of the attribute is the only valid syntax for false:

<input type="checkbox" />

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.

半枫 2024-10-10 14:27:09

实际上,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.

数理化全能战士 2024-10-10 14:27:09

空字符串通常为 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.

破晓 2024-10-10 14:27:09

checked 属性可以在 javascript 中通过以下方式设置。
(isOrdered 存储一个布尔值)

<input checked={isOrdered} type="checkbox"/> Ordered

checked attribute can be set in the following way in javascript.
(isOrdered stores a boolean value)

<input checked={isOrdered} type="checkbox"/> Ordered
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文