简体   繁体   中英

Bootstrap checkbox

I've been developing a web application using bootstrap 3.0.2 and jQuery. But I've some problems with checkboxes. Imagine that I've the following checkbox:

<label class="checkbox margin-left5">
<input type="checkbox" name="xptoName" id="xptoNameId">
Hello World
</label>

So i want to check/uncheck through jQuery. For that, I use this function:

function checking(isToCheck, id){
 $("#"+id).attr('checked', isToCheck);
}

If you call the first time this function:

checking(true, "xptoNameId");

It'll check the checkbox, but if I called several times after this, for checking and unchecking the checkbox will not check again.

Any clue for this strange behavior?

Thanks.

Use .prop() instead of .attr()

function checking(isToCheck, id) {
    $("#" + id).prop('checked', isToCheck);
}

Read .prop() vs .attr()

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