单击第一个子项后,选中所有复选框错误
check all checkbox bug after clicking first child
$("#main").click(function(e) {
$('.child').prop('checked', !$('.child').prop("checked"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="main" type="checkbox">
<br>
<br>
<input class="child" type="checkbox">
<br>
<input class="child" type="checkbox">
<br>
<input class="child" type="checkbox">
<br>
<input class="child" type="checkbox">
<br>
我在执行全部检查功能时遇到问题。当我点击#main时,一切都很好,即使切换也可以。但试着先点击第一个孩子,然后点击#main,我在那里经历了一种奇怪的行为。2、3或4个孩子的复选框不会出现这种情况,为什么会出现这种情况?
使用$(this)
代替$('.child')
。
或者使用@Balachandran建议的$('.child').prop('checked', this.checked);
:-
$("#main").click(function(e){
$('.child').prop('checked',$(this).prop("checked"));
});
$(".child").change(function(e){
if($('.child:checked').length== $('.child').length){
$('#main').prop('checked',true);
} else{
$('#main').prop('checked',false);
}
//or more short you can use
$('#main').prop('checked',$('.child:checked').length== $('.child').length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="main" type="checkbox">
<br><br>
<input class="child" type="checkbox">
<br>
<input class="child" type="checkbox"><br>
<input class="child" type="checkbox"><br>
<input class="child" type="checkbox"><br>
相关文章:
- jquery表单验证复选框错误显示
- Javascript错误,根据其他复选框的结果更新复选框/按钮
- Javascript复选框逻辑错误
- 复选框验证错误
- 量角器如果(复选框.isSelected())错误
- 检查所有选中的复选框是否具有值=正确,以及所有未选中的复选框的值=错误
- 未捕获的错误:语法错误,无法识别的表达式:输入:复选框[名称=ninja_forms_field_57[术语]]
- PHP 复选框邮件程序为每个错误
- 类型错误: 无法分配给只读属性“选中”复选框
- jQuery:计数选中的复选框 - 错误的计数
- 显示对话框时的错误复选框
- 我的JavaScript在复选框检查中有一些错误
- $(..).实时不是函数错误和复选框选中错误
- 语法错误,无法识别的表达式:[对象 HTMLDivElement]输入:复选框[名称=]:选中
- 保留Rails表单验证错误的复选框状态
- 即使选中复选框,也会出现错误
- 单击第一个子项后,选中所有复选框错误
- ExtJs复选框错误
- Angular复选框错误
- 复选框“错误”:无法获取属性“'checked';对象为空或未定义”