为什么选择/取消选择复选框只能使用jQuery一次? [英] Why select/deselect checkbox works only once with jQuery?
本文介绍了为什么选择/取消选择复选框只能使用jQuery一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的选择/取消选择选项来检查和取消选中我的网页中的复选框。它运作良好,但只有一次。我不知道有什么问题!
请查看我的代码。您可以在这里看到它的演示。
HTML代码:
< input type =checkbox id =main/>全选< br />
< input type =checkboxclass =options/> Item 1< br />
< input type =checkboxclass =options/> Item 2< br />
< input type =checkboxclass =options/> Item 3< br />
< input type =checkboxclass =options/> Item 4< br />
jQuery代码:
$('#main')。click(function(e){
$('。options')。attr('checked',this.checked);
}) ;
解决方案
使用
$('。options')。prop('checked',this.checked);
良好阅读 .prop()与.attr()
I've a simple Select/Deselect option to check and uncheck checkboxes in my page. It works well, but only once. I don't know what's the problem for!
Please take a look at my codes. You can see its demo here.
HTML codes:
<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 code:
$('#main').click(function(e) {
$('.options').attr('checked', this.checked);
});
解决方案
You need to use .prop()
instead of .attr()
Use
$('.options').prop('checked', this.checked);
A Good Read .prop() vs .attr()
这篇关于为什么选择/取消选择复选框只能使用jQuery一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文