HTML复选框的选中属性的正确值是多少?

时间:2021-12-02 09:50:31

We all know how to form a checkbox input in HTML:

我们都知道如何在HTML中形成一个复选框输入:

<input name="checkbox_name" id="checkbox_id" type="checkbox">

What I don't know -- what's the technically correct value for a checked checkbox? I've seen these all work:

我不知道的是,检查复选框的技术正确值是多少?我看过这些作品:

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

Is the answer that it doesn't matter? I see no evidence for the answer marked as correct here from the spec itself:

答案是否无关紧要?我看不出任何证据可以证明这个答案是正确的。

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful. Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property. The INPUT element is used to create a checkbox control.

复选框(和单选按钮)是可以被用户选中的开关。当控件元素的勾选属性被设置时,开关是“on”。当表单提交时,只有“on”复选框控件才能成功。表单中的几个复选框可能共享相同的控件名。因此,例如,复选框允许用户为相同的属性选择几个值。输入元素用于创建复选框控件。

What would a spec writer say is the correct answer? Please provide evidence-based answers.

一个规范的作者会说什么是正确的答案?请提供以证据为基础的答案。

7 个解决方案

#1


409  

Strictly speaking, you should put something that makes sense - according to the spec here, the most correct version is:

严格地说,你应该放一些有意义的东西——根据这里的规范,最正确的版本是:

<input name=name id=id type=checkbox checked=checked>

For HTML, you can also use the empty attribute syntax, checked="", or even simply checked (for stricter XHTML, this is not supported).

对于HTML,您还可以使用空属性语法,检查=“”,或者甚至简单地检查(对于更严格的XHTML,这是不支持的)。

Effectively, however, most browsers will support just about any value between the quotes. All of the following will be checked:

然而,实际上,大多数浏览器将支持引号之间的任何值。以下各项将会被核对:

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

And only the following will be unchecked:

只有以下内容将被取消:

<input name=name id=id type=checkbox>

See also this similar question on disabled="disabled".

请参见关于disabled="disabled"的类似问题。

#2


40  

HTML5 spec:

HTML5规范:

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

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 :

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.

如果属性存在,则它的值必须是空字符串或一个值,该值是对属性的规范名称的ASCII大小写不敏感的匹配,不带任何引导或尾随空格。

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:

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

<input />

Recommendation

建议

If you care about writing valid XHTML, use checked="checked", since <input checked> is invalid XHTML (but valid HTML) and other alternatives are less readable. Else, just use <input checked> as it is shorter.

如果您关心编写有效的XHTML,请使用checked="checked",因为是无效的XHTML(但有效的HTML),其他替代方法的可读性较差。否则,只需使用,因为它更短。

#3


34  

<input ... checked />
<input ... checked="checked" />

Those are equally valid. And in JavaScript:

这些都是同样有效的。在JavaScript中:

input.checked = true;
input.setAttribute("checked");
input.setAttribute("checked","checked");

#4


7  

you want this i think: checked='checked'

你想要这个,我想:勾选='勾选'

#5


5  

  1. checked
  2. 检查
  3. checked=""
  4. 检查= " "
  5. checked="checked"

    检查= "检查"

    are equivalent;

    是等价的;


according to spec checkbox '----ⓘ checked = "checked" or "" (empty string) or empty Specifies that the element represents a selected control.---'

根据规范复选框的检查——ⓘ=“检查”或“(空字符串)或空指定元素代表一个选择control.——”

#6


2  

It's pretty crazy town that the only way to make checked false is to omit any values. With Angular 1.x, you can do this:

它是相当疯狂的城镇,唯一的方法使检查错误是省略任何值。1角。x,你可以这样做:

  <input type="radio" ng-checked="false">

which is a lot more sane, if you need to make it unchecked.

如果你需要让它不受约束的话,这就更加合理了。

#7


-2  

Well, to use it i dont think matters (similar to disabled and readonly), personally i use checked="checked" but if you are trying to manipulate them with JavaScript, you use true/false

我个人认为使用它并不重要(类似于禁用和只读),我使用了checked="checked",但是如果你想用JavaScript操作它们,你就使用true/false

#1


409  

Strictly speaking, you should put something that makes sense - according to the spec here, the most correct version is:

严格地说,你应该放一些有意义的东西——根据这里的规范,最正确的版本是:

<input name=name id=id type=checkbox checked=checked>

For HTML, you can also use the empty attribute syntax, checked="", or even simply checked (for stricter XHTML, this is not supported).

对于HTML,您还可以使用空属性语法,检查=“”,或者甚至简单地检查(对于更严格的XHTML,这是不支持的)。

Effectively, however, most browsers will support just about any value between the quotes. All of the following will be checked:

然而,实际上,大多数浏览器将支持引号之间的任何值。以下各项将会被核对:

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

And only the following will be unchecked:

只有以下内容将被取消:

<input name=name id=id type=checkbox>

See also this similar question on disabled="disabled".

请参见关于disabled="disabled"的类似问题。

#2


40  

HTML5 spec:

HTML5规范:

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

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 :

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.

如果属性存在,则它的值必须是空字符串或一个值,该值是对属性的规范名称的ASCII大小写不敏感的匹配,不带任何引导或尾随空格。

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:

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

<input />

Recommendation

建议

If you care about writing valid XHTML, use checked="checked", since <input checked> is invalid XHTML (but valid HTML) and other alternatives are less readable. Else, just use <input checked> as it is shorter.

如果您关心编写有效的XHTML,请使用checked="checked",因为是无效的XHTML(但有效的HTML),其他替代方法的可读性较差。否则,只需使用,因为它更短。

#3


34  

<input ... checked />
<input ... checked="checked" />

Those are equally valid. And in JavaScript:

这些都是同样有效的。在JavaScript中:

input.checked = true;
input.setAttribute("checked");
input.setAttribute("checked","checked");

#4


7  

you want this i think: checked='checked'

你想要这个,我想:勾选='勾选'

#5


5  

  1. checked
  2. 检查
  3. checked=""
  4. 检查= " "
  5. checked="checked"

    检查= "检查"

    are equivalent;

    是等价的;


according to spec checkbox '----ⓘ checked = "checked" or "" (empty string) or empty Specifies that the element represents a selected control.---'

根据规范复选框的检查——ⓘ=“检查”或“(空字符串)或空指定元素代表一个选择control.——”

#6


2  

It's pretty crazy town that the only way to make checked false is to omit any values. With Angular 1.x, you can do this:

它是相当疯狂的城镇,唯一的方法使检查错误是省略任何值。1角。x,你可以这样做:

  <input type="radio" ng-checked="false">

which is a lot more sane, if you need to make it unchecked.

如果你需要让它不受约束的话,这就更加合理了。

#7


-2  

Well, to use it i dont think matters (similar to disabled and readonly), personally i use checked="checked" but if you are trying to manipulate them with JavaScript, you use true/false

我个人认为使用它并不重要(类似于禁用和只读),我使用了checked="checked",但是如果你想用JavaScript操作它们,你就使用true/false