HTML 复选框的选中属性的正确值是多少?
我们都知道如何在 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">
答案是不重要吗?我在此处没有看到标记为正确的答案的证据< /a> 来自 规范 本身:
复选框(和单选按钮)是可以切换的开/关开关 由用户。当控制元素被选中时,开关处于“打开”状态 属性已设置。提交表单时,仅“打开”复选框 控制才能成功。一个表单中的多个复选框可以共享 相同的控件名称。因此,例如,复选框允许用户 为同一属性选择多个值。使用 INPUT 元素 创建一个复选框控件。
规范编写者会说正确答案是什么?请提供基于证据的答案。
We all know how to form a checkbox input in 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.
What would a spec writer say is the correct answer? Please provide evidence-based answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
严格来说,您应该根据规范此处放置一些有意义的内容< /a>,最正确的版本是:
对于 HTML,您还可以使用 空属性语法,
checked=""
,甚至简单的checked
(对于更严格的XHTML,这是不支持)。然而,实际上,大多数浏览器都支持引号之间的任何值。将检查以下所有内容:
并且仅取消选中以下内容:
另请参阅
disabled="disabled" 上的类似问题
。Strictly speaking, you should put something that makes sense - according to the spec here, the most correct version is:
For HTML, you can also use the empty attribute syntax,
checked=""
, or even simplychecked
(for stricter XHTML, this is not supported).Effectively, however, most browsers will support just about any value between the quotes. All of the following will be checked:
And only the following will be unchecked:
See also this similar question on
disabled="disabled"
.HTML5 规范:
http://www .w3.org/TR/html5/forms.html#attr-input-checked:
http://www.w3.org/TR/html5/infrastruct.html #布尔属性:
结论:
以下是有效、等价和正确:
以下是无效:
缺少该属性是唯一有效的语法>false:
建议
如果您关心编写有效的 XHTML,请使用
checked="checked"
,因为
是无效的 XHTML(但有效的 HTML),其他替代方案是可读性较差。否则,只需使用
因为它更短。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 XHTML (but valid HTML) and other alternatives are less readable. Else, just use<input checked>
as it is shorter.这些同样有效。在 JavaScript 中:
Those are equally valid. And in JavaScript:
你想要这个我想:
已检查='已检查'
you want this i think:
checked='checked'
检查=“检查”
是等价的;
根据规范 复选框
'----ⓘchecked=“checked”或“”(空字符串)或空
指定该元素代表选定的控件。---'
checked="checked"
are equivalent;
according to spec checkbox
'----ⓘ checked = "checked" or "" (empty string) or empty
Specifies that the element represents a selected control.---'
这是一个非常疯狂的小镇,使检查为假的唯一方法是省略任何值。使用 Angular 1.x,你可以这样做:
如果你需要不检查它,这会更明智。
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:
which is a lot more sane, if you need to make it unchecked.
就像所有输入一样,这个输入也有一个“值”属性。如果您将
value
属性与name
属性一起设置,它将在标头中发送${name}=${value}
。假设我们有一个包含一堆内容的表单,我们希望用户决定他们拥有什么角色,我们可以使用一个复选框。最简单的方法是设置将传递到服务器的“value”属性。像这样:服务器将收到以下内容:
my_checkbox=is_person
但仅当复选框被选中时。这将是
my_checkbox=
如果它是空的。
这与无线电输入完全相同。
但是..我有一种感觉,这不是你所要求的...所以只要把我写的另一个答案写在下面:
如果
checked
属性设置为任何内容(甚至false )无论如何,它都会被激活。
checked
属性没有指定值,如果将其设置为任何值,它将默认为“true”(即使您将其设置为false
)。例如:
即使设置为 false,也会被检查。
这样做也会检查它。没关系——只要设置了就会被检查。
希望这能回答您的问题,无论属性的值是什么,
checked
属性都会检查输入。您可以在这里测试它: https://battledash-2.github.io/Live-IDE /
或像 repl.it 这样的任何地方,或本地。
Just like all input's, this one also has a 'value' attribute. If you set the
value
attribute along with thename
attribute, it will send${name}=${value}
in the headers. Let's say we had a form with a bunch of stuff and we wanted the user to decide what roles they have, we could use a checkbox. The easiest way is to set the 'value' attribute which will be passed along to the server. Like this:The server will receive the following:
my_checkbox=is_person
but only if the checkbox is checked. This would be
my_checkbox=
if it is empty.
This is the exact same with
radio
inputs.But.. I have a feeling this isn't what you're asking for... So just encase I'll write another answer below:
If the
checked
attribute is set to anything (evenfalse
) it will be activated, no matter what. Thechecked
attribute doesn't have a specified value, if it's set to anything it will default to 'true' (even if you set it tofalse
).For example:
Will be checked even though it's set to false.
Doing this will also check it. It doesn't matter -- as long as it's set it will be checked.
Hope this answers your question, the
checked
attribute will check the input no matter the value of the attribute.You can test it here: https://battledash-2.github.io/Live-IDE/
or anywhere like repl.it, or locally.
我认为这可能有帮助:
First read all the specs from Microsoft and W3.org.
You'd see that the setting the actual element of a checkbox needs to be done on the ELEMENT PROPERTY, not the UI or attribute.
$('mycheckbox')[0].checked
其次,您需要知道检查的属性返回字符串“true”,“false”
为什么这很重要?因为你需要使用正确的Type。字符串,而不是布尔值。这在解析复选框时也很重要。
$('mycheckbox')[0].checked = "true"
if($('mycheckbox')[0].checked === "true"){
//做某事
}
您还需要意识到“checked”ATTRIBUTE 用于最初设置复选框的值。一旦元素渲染到 DOM 中,这并没有多大作用。想象一下当网页加载并最初解析时此工作原理。
我将按照 IE 的偏好进行选择:
最后,复选框的主要混乱之处在于复选框 UI 元素与元素的属性值不同。它们不直接相关。
如果您使用 .net,您会发现用户“选中”复选框永远不会反映传递给控制器的实际 bool 值。
为了设置 UI,我使用
$('mycheckbox').val(true);
和$('mycheckbox').attr('checked', 'checked');< /代码>
In short, for a checked checkbox you need:
Initial DOM:
<input type="checkbox" checked="checked">
Element Property:
$('mycheckbox')[0].checked = "true";
UI:
$('mycheckbox').val(true);
and$('mycheckbox').attr('checked', 'checked');
I think this may help:
First read all the specs from Microsoft and W3.org.
You'd see that the setting the actual element of a checkbox needs to be done on the ELEMENT PROPERTY, not the UI or attribute.
$('mycheckbox')[0].checked
Secondly, you need to be aware that the checked attribute RETURNS a string "true", "false"
Why is this important? Because you need to use the correct Type. A string, not a boolean. This also important when parsing your checkbox.
$('mycheckbox')[0].checked = "true"
if($('mycheckbox')[0].checked === "true"){
//do something
}
You also need to realize that the "checked" ATTRIBUTE is for setting the value of the checkbox initially. This doesn't do much once the element is rendered to the DOM. Picture this working when the webpage loads and is initially parsed.
I'll go with IE's preference on this one:
<input type="checkbox" checked="checked"/>
Lastly, the main aspect of confusion for a checkbox is that the checkbox UI element is not the same as the element's property value. They do not correlate directly.
If you work in .net, you'll discover that the user "checking" a checkbox never reflects the actual bool value passed to the controller.
To set the UI, I use both
$('mycheckbox').val(true);
and$('mycheckbox').attr('checked', 'checked');
In short, for a checked checkbox you need:
Initial DOM:
<input type="checkbox" checked="checked">
Element Property:
$('mycheckbox')[0].checked = "true";
UI:
$('mycheckbox').val(true);
and$('mycheckbox').attr('checked', 'checked');
已选中的复选框在技术上的正确值为零 (0),并且当未选中该复选框时,未定义其索引。
The technically correct value for a checked checkbox is zero (0), and when the checkbox is not checked, it's index is not defined.
好吧,我认为使用它并不重要(类似于禁用和只读),我个人使用checked =“checked”,但如果您尝试使用JavaScript操作它们,您可以使用true/false
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