Html 选中和选中的哪些值是假的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4228658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:21:22  来源:igfitidea点击:

What values for checked and selected are false?

htmlw3c

提问by mpen

I think according to W3 spec, you're supposed to do

我认为根据 W3 规范,你应该这样做

<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?

但是,大多数浏览器都会接受它,您只需编写“CHECKED”而不给它赋值。那么,如果您确实包含该属性,是否有任何值(始终)被认为是错误的?

回答by mpen

There are no valuesthat will cause the checkbox to be unchecked. If the checkedattribute exists, the checkbox will be checked regardless of what value you set it to.

没有价值,这将导致该复选框是选中。如果该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" />

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

渲染所有现代浏览器(FF3.6、Chrome 10、IE8)中检查的所有内容。

回答by Quentin

The checkedand selectedattributes 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.

checkedselected属性被允许只有两个值,它们是属性名称的副本和(来自HTML 5起)一个空字符串。给出任何其他值都是错误的。

If you don't want to set the attribute, then the entire attribute must be omitted.

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

Note that in HTML 4you may omit everything except the value. HTML 5changed this to omit everything except the name (which makes no practical difference).

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

Thus, the complete (aside from variations in cAsE) set of valid representations of the attribute are:

因此,该属性的完整(除了 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 -->

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.

作为 text/html(HTML 或 XHTML)的文档将通过标签汤解析器提供,并且已检查属性(具有任何值)的存在将被视为“应检查此元素”。因此,虽然无效,checked="true", checked="yes", 和checked="false"都会触发选中状态。

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 expectthat 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.

如果给属性赋予不同的值,我没有任何兴趣去找出用于 XML 解析模式的错误恢复机制,但我希望HTML 的遗产和/或简单的错误恢复会在相同的方式:如果属性存在,则检查元素。

(And all the above applies equally to selectedas it does to checked.)

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

回答by Orbling

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.

显然,空字符串在所有浏览器中都没有被视为空字符串,并且检查属性的存在被视为已检查。所以整个属性必须存在或省略。

回答by jwheron

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.

实际上,HTML 4.01 规范说这些属性不需要值。我个人还没有遇到过提供值将这些控件呈现为未选中状态的情况。

Here are the respective links to the spec document for selectedand checked.

以下是selectedchecked的规范文档的相应链接。

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.

编辑:Firebug 将复选框呈现为选中状态,而不管我为已选中的属性加上引号(包括仅键入“已选中”而没有任何值),并且 IE 8 的开发人员工具会强制选中“已选中”。但是,我不确定其他浏览器是否存在任何可能改变复选框呈现状态的类似工具。