为什么选择/取消选择复选框只能使用jQuery一次?

时间:2014-07-23 12:31:26

标签: jquery html checkbox html-select deselect

我选择了一个简单的选择/取消选择选项来检查和取消选中我页面中的复选框。它效果很好,但只有一次。我不知道问题是什么!
请看一下我的代码。你可以看到它的demo here

HTML code:

<input type="checkbox" id="main" />Select All<br />
<input type="checkbox" class="options" />Item 1<br />
<input type="checkbox" class="options" />Item 2<br />
<input type="checkbox" class="options" />Item 3<br />
<input type="checkbox" class="options" />Item 4<br />

jQuery代码:

$('#main').click(function(e) {
    $('.options').attr('checked', this.checked);
});

1 个答案:

答案 0 :(得分:3)

您需要使用.prop()代替.attr()

使用

$('.options').prop('checked', this.checked);

好读.prop() vs .attr()