繁体   English   中英

jQuery按钮检查所有复选框问题

[英]jQuery button to check all check boxes issue

我有一个按钮,可检查div所有复选框并取消选中。

但是,如果要手动选中一个复选框,然后单击“全部选中”按钮,然后全部取消选中,则手动选中的复选框不会变为未选中状态!

有任何想法吗?

http://jsfiddle.net/hM5bu/1/

那是因为jQuery在1.6中进行了更改

使用attr代替prop是打破它的方法。

尝试使用prop代替

更新的小提琴: http : //jsfiddle.net/hM5bu/2/

请参阅以下问题: .prop()与.attr(),以获取有关jQuery 1.6中propattr更多信息

这是一个解决方案:

<input type="checkbox" name="todos" id="todos" /> All<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="1" />1<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="2" />2<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="3" />3<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="4" />4<br/>

<script type="text/javascript">

$(function() {

$("#todos").click(function() {                          
if ($(this).is(':checked'))                         
$(".marcartodos").attr('checked', true);
else 
$(".marcartodos").attr('checked', false);
});

});

</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM