javascript - uncheck checkbox which checked by user - Stack Overflow

When i checkuncheck checkboxes and radiobuttons, browser doesnt set attribute checked. So, when i am t

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?

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?

Share asked May 13, 2011 at 15:00 Neir0Neir0 13.4k28 gold badges90 silver badges143 bronze badges 2
  • 4 Since the API for this stuff changed recently, you should specify whether you're using jQuery 1.5.x or under, or jQuery 1.6 or above. – Jamie Wong Commented May 13, 2011 at 15:04
  • @Jamie Wong i'm using jQuery 1.6 – Neir0 Commented May 13, 2011 at 15:12
Add a ment  | 

3 Answers 3

Reset to default 5

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742406196a4437895.html

相关推荐

  • javascript - uncheck checkbox which checked by user - Stack Overflow

    When i checkuncheck checkboxes and radiobuttons, browser doesnt set attribute checked. So, when i am t

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信