What's the proper value for a checked attribute of an HTML checkbox?

后端 未结 8 2033
栀梦
栀梦 2020-11-22 13:02

We all know how to form a checkbox input in HTML:


What I don\'t k

相关标签:
8条回答
  • 2020-11-22 13:16
    <input ... checked />
    <input ... checked="checked" />
    

    Those are equally valid. And in JavaScript:

    input.checked = true;
    input.setAttribute("checked");
    input.setAttribute("checked","checked");
    
    0 讨论(0)
  • 2020-11-22 13:23

    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 讨论(0)
  • 2020-11-22 13:30
    1. checked
    2. checked=""
    3. checked="checked"

      are equivalent;


    according to spec checkbox '----ⓘ checked = "checked" or "" (empty string) or empty Specifies that the element represents a selected control.---'

    0 讨论(0)
  • 2020-11-22 13:30

    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:

      <input type="radio" ng-checked="false">
    

    which is a lot more sane, if you need to make it unchecked.

    0 讨论(0)
  • 2020-11-22 13:32

    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

    0 讨论(0)
  • 2020-11-22 13:38

    Strictly speaking, you should put something that makes sense - according to the spec here, the most correct version is:

    <input name=name id=id type=checkbox checked=checked>
    

    For HTML, you can also use the empty attribute syntax, checked="", or even simply checked (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:

    <input name=name id=id type=checkbox checked>
    <input name=name id=id type=checkbox checked="">
    <input name=name id=id type=checkbox checked="yes">
    <input name=name id=id type=checkbox checked="blue">
    <input name=name id=id type=checkbox checked="false">
    

    And only the following will be unchecked:

    <input name=name id=id type=checkbox>
    

    See also this similar question on disabled="disabled".

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