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

前端 未结 9 1353
故里飘歌
故里飘歌 2020-12-05 18:09

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 ex

相关标签:
9条回答
  • 2020-12-05 18:25

    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.

    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.

    0 讨论(0)
  • 2020-12-05 18:29

    The presence of the "checked" property specifies the status. The value is irrelevant/not needed.

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

    is my suggestion

    0 讨论(0)
  • 2020-12-05 18:31

    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.

    0 讨论(0)
  • 2020-12-05 18:35

    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.

    0 讨论(0)
  • 2020-12-05 18:38

    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" />
    
    0 讨论(0)
  • 2020-12-05 18:40

    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?

    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.

    0 讨论(0)
提交回复
热议问题