简体   繁体   中英

How do I turn ON/OFF input with type = checkbox using javascript

I have an input type checkbox that is configured for on/off purposes, with values true/false accordingly. I want to be able to turn it off and on whenever i call a function using javascript.

The checkbox looks like this:

<div class="onoffswitch">
    <input type="hidden" id="i" value="{{bc}}"/>
    <input type="checkbox" data-type="bc-s" name="onoffswitch{{itemID}}-age" class="onoffswitch-checkbox bc-manage" id="asd" {{#bc}}checked{{/bc}} />
    <label id="sbc" class="onoffswitch-label" for="myonoffswitch{{itemID}}-age">
        <div class="onoffswitch-inner"></div>
        <div class="onoffswitch-switch"></div>
    </label>
</div>

I want to turn it on/off using this:

function disregardSave () {
   container.find("#asd").prop('checked', data.bc);
}

but failed then tried this:

function disregardSave () {
   container.find("#asd").val(data.bc);
}

and nothing works. I don't know why.

Checkbox's id is "asd" so have you tried :

$('#asd').prop("checked", true);

This is my way to change checkbox status in jquery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM