jQuery HTML 中布尔属性(例如选中的复选框)的语法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2874949/
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 syntax for boolean attributes, e.g. a checked checkbox, in HTML?
提问by Fiona - myaccessible.website
Sounds like a bit of a silly question, but I am wondering what is the best way of stating that a checkbox is checked/unchecked in HTML.
听起来有点愚蠢的问题,但我想知道在 HTML 中选中/取消选中复选框的最佳方式是什么。
I have seen many different examples:
我见过很多不同的例子:
<input type="checkbox" checked="checked" />
<input type="checkbox" />
<input type="checkbox" checked="yes" />
<input type="checkbox" checked="no" />
<input type="checkbox" checked="true" />
<input type="checkbox" checked="false" />
Which browsers work with which ones of these, and most importantly, does jQuery figure out which box is checked in all 3?
哪些浏览器适用于其中的哪些,最重要的是,jQuery 是否确定在所有 3 个中选中了哪个框?
Edit:The W3C spec seems to imply that just the checked attr being there is correct. Does that mean that checked="false" and checked="no" will still check the box though?
编辑:W3C 规范似乎暗示只有检查过的属性是正确的。这是否意味着 checked="false" 和 checked="no" 仍然会选中该框?
回答by Andy E
In HTML:
在 HTML 中:
<input type="checkbox" checked>
<input type="checkbox" checked="checked">
For XHTML you have to use attribute/value matching pairs:
对于 XHTML,您必须使用属性/值匹配对:
<input type="checkbox" checked="checked" />
回答by Ondrej Slinták
HTML:
HTML:
Any of them should be the right one as they say on W3C page. checked
attribute just has to be set.
正如他们在W3C 页面上所说,他们中的任何一个都应该是正确的。checked
属性只需要设置。
As I wrote in Coronatus' comment:
正如我在 Coronatus 的评论中所写:
In fact, it doesn't matter if it's checked, checked="whatever" or checked="checked". It's checking boolean value, so it's either true (set) or false (not set).
事实上,无论是checked、checked="whatever"还是checked="checked"都没有关系。它正在检查布尔值,因此它是真(设置)或假(未设置)。
XHTML:
XHTML:
You have to specify it, so checked="checked"
is the only valid one. Most of the browsers will probably parse HTML values correct, but you'll have error on your page nonetheless.
您必须指定它,因此checked="checked"
是唯一有效的。大多数浏览器可能会正确解析 HTML 值,但您的页面仍然会出错。
回答by Amy B
回答by user113716
A quick test reveals that jQuery equates any value set to being checked.
快速测试表明 jQuery 将任何值设置为被检查。
alert( $('input:checked').length );
// Returns 5
Even an empty string and the negative values.
即使是空字符串和负值。
回答by Justin Gregtheitroade
I would be careful using it on chrome, firefox and opera. Chorme seems to have a problem with checkboxes showing up, Firefox is case sensitive with styles and tags, and opera seems to run fine but is slow at updating. Might just be the versions of the browsers being used.
我会小心地在 chrome、firefox 和 opera 上使用它。Chorme 似乎在显示复选框方面存在问题,Firefox 对样式和标签区分大小写,opera 似乎运行良好但更新缓慢。可能只是正在使用的浏览器的版本。
Other than that I believe that it should run <input type="checkbox" checked />
just fine in all of them. The only difference really is how the individual browsers appear to show the content on the page.
除此之外,我相信它应该<input type="checkbox" checked />
在所有这些中都运行良好。唯一的区别实际上是各个浏览器在页面上显示内容的方式。
回答by Ms2ger
The W3C spec seems to imply that just the checked attr being there is correct. Does that mean that checked="false" and checked="no" will still check the box though?
W3C 规范似乎暗示只有检查过的属性是正确的。这是否意味着 checked="false" 和 checked="no" 仍然会选中该框?
Exactly. That's why it's a bad idea to use checked="true"
and checked="yes"
, they imply that checked="false"
and checked="no"
will not check the box.
确切地。这就是为什么使用checked="true"
and是一个坏主意的原因checked="yes"
,它们暗示checked="false"
并且checked="no"
不会选中该框。
回答by hudolejev
According to W3C recommendationsyour first suggestion is correct.
根据W3C 的建议,您的第一个建议是正确的。
回答by kazanaki
The presence of the "checked" propertyspecifies the status. The value is irrelevant/not needed.
“已检查”属性的存在指定了状态。该值无关紧要/不需要。
<input type="checkbox" checked="checked" />
<input type="checkbox" />
is my suggestion
是我的建议