Html 在 HTML5 中选中和取消选中复选框的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12700626/
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
What is the proper way to check and uncheck a checkbox in HTML5?
提问by B Seven
Looked at the HTML spec, but couldn't make heads or tails of it: http://www.w3.org/TR/html5/the-input-element.html#attr-input-checked
查看了 HTML 规范,但无法理解它:http: //www.w3.org/TR/html5/the-input-element.html#attr-input-checked
What is the correct way to check a checkbox in HTML (not dynamically)?
在 HTML(非动态)中检查复选框的正确方法是什么?
checked="true"
checked="checked"
What is the correct way to unchecka checkbox?
取消选中复选框的正确方法是什么?
<input type="checkbox" /> with no checked attribute
checked="false"
checked="none"
Where to check the HTML specification to check/uncheck a checkbox?
在哪里检查 HTML 规范以选中/取消选中复选框?
回答by valentinas
<input type="checkbox" checked="checked" />
or simply
或者干脆
<input type="checkbox" checked />
for checked checkbox.
No checked attribute (<input type="checkbox" />
) for unchecked checkbox.
对于选中的复选框。<input type="checkbox" />
对于未选中的复选框,没有选中的属性 ( )。
reference: http://www.w3.org/TR/html-markup/input.checkbox.html#input.checkbox.attrs.checked
参考:http: //www.w3.org/TR/html-markup/input.checkbox.html#input.checkbox.attrs.checked
回答by Jukka K. Korpela
According to HTML5 drafts, the checked
attribute is a “boolean attribute”, and “The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.” It is the name of the attribute that matters, and suffices. Thus, to make a checkbox initially checked, you use
根据 HTML5 草案,该checked
属性是“布尔属性”,“元素上布尔属性的存在代表真值,不存在该属性代表假值”。重要的是属性的名称,并且就足够了。因此,要使复选框最初被选中,您可以使用
<input type=checkbox checked>
By default, in the absence of the checked
attribute, a checkbox is initially unchecked:
默认情况下,在没有checked
属性的情况下,复选框最初是未选中的:
<input type=checkbox>
Keeping things this way keeps them simple, but if you need to conform to XML syntax (i.e. to use HTML5 in XHTML linearization), you cannot use an attribute name alone. Then the allowed (as per HTML5 drafts) values are the empty string and the string checked
, case insensitively. Example:
保持这种方式使它们变得简单,但是如果您需要符合 XML 语法(即在 XHTML 线性化中使用 HTML5),则不能单独使用属性名称。然后允许的(根据 HTML5 草稿)值为空字符串和字符串checked
,不区分大小写。例子:
<input type="checkbox" checked="checked" />
回答by secretformula
<input type="checkbox" checked />
<input type="checkbox" checked />
HTML5 does not require attributes to have values
HTML5 不要求属性具有值
回答by Robert
In jQuery:
在 jQuery 中:
To check the checkbox:
要选中复选框:
$("#checkboxid").attr("checked","checked");
To uncheck the checkbox:
要取消选中复选框:
$("#checkboxid").removeAttr("checked");
The other answers hint at the solution and point you to documentation that after further digging will get you to this answer. Jukka K. Korpela has the reason this is the correct answer, basically I followed his link and then looked up the jQuery docs to get to that result. Just figured I'd save future people who find this article those extra steps.
其他答案暗示了解决方案,并为您指明文档,在进一步挖掘后,您将获得此答案。Jukka K. Korpela 认为这是正确答案的原因,基本上我是按照他的链接,然后查找 jQuery 文档以获得该结果。只是想我会为以后找到这篇文章的人节省这些额外的步骤。
回答by nicolallias
Complementary answer to Robert's answer http://jsfiddle.net/ak9Sb/in jQuery
jQuery 中罗伯特回答http://jsfiddle.net/ak9Sb/ 的补充回答
When getting/setting checkbox state, one may encounter these phenomenons:
在获取/设置复选框状态时,可能会遇到以下现象:
.trigger("click");
Does check an unchecked checkbox, but do not add the checked attribute. If you use triggers, do not try to get the state with "checked" attribute.
选中未选中的复选框,但不添加选中的属性。如果您使用触发器,请不要尝试使用“已检查”属性获取状态。
.attr("checked", "");
Does not uncheck the checkbox...
不要取消选中复选框...
回答by Bruno Vieira
You can refer to this page at w3schoolsbut basically you could use any of:
您可以在w3schools 上参考此页面,但基本上您可以使用以下任何一种:
<input checked>
<input checked="checked">
<input checked="">
回答by N1gthm4r3
you can use autocomplete="off" on parent form, so if you reload your page, checkboxes will not be checked automatically
您可以在父表单上使用 autocomplete="off",因此如果您重新加载页面,复选框将不会自动选中
回答by Michael Durrant
<form name="myForm" method="post">
<p>Activity</p>
skiing: <input type="checkbox" name="activity" value="skiing" checked="yes" /><br />
skating: <input type="checkbox" name="activity" value="skating" /><br />
running: <input type="checkbox" name="activity" value="running" /><br />
hiking: <input type="checkbox" name="activity" value="hiking" checked="yes" />
</form>