简体   繁体   中英

uncheck checkbox which checked by user

When i check\\uncheck checkboxes and radiobuttons, browser doesnt set attribute checked. So, when i am trying uncheck checkbox using jquery:

$('#tab2 input').removeAttr('checked');

it doesnt work. It works case checkbox was checked from my javascript code. How can i uncheck checkbox which already checked by user?

You're setting a property, so use .prop() in jQuery 1.6 .

$('#tab2 input').prop('checked',false);

Or if there's only one input , just change the property of the element directly.

$('#tab2 input')[0].checked = false;

When jQuery 1.6.1 is released, Or upgrade to jQuery 1.6.1 and you'll be able to go back to using .attr() to set properties again, but .prop() will still be the better way to do it.

From the release notes:

Specifically, boolean attributes such as checked, selected, readonly, and disabled in 1.6.1 will be treated just as they used to be treated in jQuery versions prior to 1.6. This means that code such as

 $(“:checkbox”).attr(“checked”, true); $(“option”).attr(“selected”, true); $(“input”).attr(“readonly”, true); $(“input”).attr(“disabled”, true); 

or even:

 if ( $(“:checkbox”).attr(“checked”) ) { /* Do something */ } 

will not need to be changed in 1.6.1 in order to work as previously expected.

$('#tab2 input').attr('checked',false);
$('input[name=foo]').attr('checked', false);

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