What is the syntax for boolean attributes, e.g. a checked checkbox, in HTML?

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.

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?

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?


In HTML:

<input type="checkbox" checked>
<input type="checkbox" checked="checked">

For XHTML you have to use attribute/value matching pairs:

<input type="checkbox" checked="checked" />

HTML:

Any of them should be the right one as they say on W3C page. checked attribute just has to be set.

As I wrote in Coronatus' comment:

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

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.


According to the HTML spec, the correct one is:

<input type="checkbox" checked />
<input type="checkbox" />

I use this and jQuery works perfectly with them.

链接地址: http://www.djcxy.com/p/9574.html

上一篇: 什么值检查和选择是错误的?

下一篇: 布尔属性的语法是什么,例如HTML中的checked复选框?