Correct way to do HTML5 checkbox

前端 未结 2 1005
醉话见心
醉话见心 2021-02-02 05:52

I can\'t seem to find an example anywhere... what\'s the correct way of doing a HTML5 checkbox?

相关标签:
2条回答
  • 2021-02-02 06:28

    A more complete example - and avoiding the long stream of posts to How to check whether a checkbox is checked in jQuery?.

    HTML

    <input id="your_id" name="your_name" value="your_value" type="checkbox">
    

    Optionally add the 'checked' attribute to default to checked on load.

    <input id="your_id" name="your_name" value="your_value" type="checkbox" checked>
    

    JavaScript

    $('#your_id').is(':checked')        // Returns a Boolean TRUE if checked
    

    e.g.

    if ($('#your_id').is(':checked')) {
        // Checkbox was checked
    }
    
    0 讨论(0)
  • 2021-02-02 06:39

    As far as I know and the docs state, nothing fundamental has changed. The basic markup is

    <input name="your_name" value="your_value" type="checkbox">
    

    What is new is some interesting properties.

    • form - a reference to the form the control is associated with (nice!)
    • autofocus - to be focused as soon as the document is loaded (nice!)
    • required - require that it be checked (super nice! Although it isn't supported by Internet Explorer or Safari (yet).)
    0 讨论(0)
提交回复
热议问题