Input Checkbox checked by default

后端 未结 5 1261
清酒与你
清酒与你 2021-02-18 16:12

For the life of me I cant set my CheckBox to checked at page load. I have tried adding value=\"true\" and Checked=\"checked\" after id and still noth

相关标签:
5条回答
  • 2021-02-18 16:24

    The attribute checked="checked" does work and the shorthand notation is just checked.

    <div class="onoffswitch-container">
    <span class="onoffswitch-title">Open Inline</span>
    <span class="onoffswitch">
    <input type="checkbox" class="onoffswitch-checkbox" id="inline" checked="checked"/> 
    <label class="onoffswitch-label" for="inline">
    <span class="onoffswitch-inner" data-swchoff-text="OFF" data-swchon-text="ON"></span>
    <span class="onoffswitch-switch"></span>
    </label>
     </span>
    </div>

    0 讨论(0)
  • 2021-02-18 16:33

    added "true" parameter below, just try it before, and it works for me

    <input type="checkbox" class="onoffswitch-checkbox" id="inline" checked="true"> 
    
    0 讨论(0)
  • 2021-02-18 16:36

    I tried all above solutions and nothing worked. It did give me some terminology to continue my research. What I ended up doing was going to the bottom of the code and added $('#inline').prop('checked', true); This solved my problem.

    0 讨论(0)
  • 2021-02-18 16:37

    just write "checked" and it works

     <input type="checkbox" class="onoffswitch-checkbox" id="inline" checked> 
    
    0 讨论(0)
  • 2021-02-18 16:39

    You're looking for the checked content attribute. Here's the relevant snippet from the official documentation: (Living Standard):

    The checked content attribute is a boolean attribute that gives the default checkedness of the input element. When the checked content attribute is added, if the control does not have dirty checkedness, the user agent must set the checkedness of the element to true; when the checked content attribute is removed, if the control does not have dirty checkedness, the user agent must set the checkedness of the element to false.

    Note the docs have links to definition of relevant terms.

    If it seems too technical, you can always go to MDN page on checked. MDN contributors have put up considerable effort into "translating" HTML Standard into more readable/understandable form (often times with examples), especially useful to people without a solid foundation on web related terminology and concepts.

    Note: You'll also find the direct answer to your question on MDN.

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