如果选中所有子复选框,如何选中父复选框 - 在文档就绪时
how to make parent checkbox checked if all child checkboxes are checked - on document ready?
我有几组复选框,它们的工作方式类似于这个jsFiddle:
http://jsfiddle.net/3vker/
基本上,如果选中所有子复选框,则选中父复选框,如果未选中任何子复选框,则取消选中父复选框。
它工作得很好,除了一个问题:
- 该页面是交互式的,因此可以根据从数据库中提取的结果选中所有子复选框(通过 HTML 中的 PHP)。在这种情况下,页面加载时选中了所有子复选框,但未选中父复选框。
我如何实现当页面完全加载并选中所有子复选框(通过 HTML 中的 PHP)时,该组的父复选框也被选中?
这是 HTML:
<form>
<fieldset>
<input type="checkbox" class="parentCheckBox" /> Africa
<div class="content">
<input type="checkbox" value="" name="countries_option[]" class="childCheckBox" <?php if (in_array("algeria", $is_checked)) {?>checked="checked"<?php }?>/> Algeria<br />
<input type="checkbox" value="" name="countries_option[]" class="childCheckBox" <?php if (in_array("namibia", $is_checked)) {?>checked="checked"<?php }?>/> Namibia<br />
</div>
</fieldset>
<p></p>
<fieldset>
<input type="checkbox" class="parentCheckBox" /> Europe
<div class="content">
<input type="checkbox" value="" name="countries_option[]" class="childCheckBox" <?php if (in_array("unitedkingdom", $is_checked)) {?>checked="checked"<?php }?>/> United Kingdom<br />
<input type="checkbox" value="" name="countries_option[]" class="childCheckBox" <?php if (in_array("italy", $is_checked)) {?>checked="checked"<?php }?>/> Italy<br />
</div>
</fieldset>
</form>
和jQuery:
$(document).ready(
function() {
$('input.childCheckBox').change(function() {
$(this).closest('fieldset').find('.parentCheckBox').prop('checked',
$(this).closest('.content').find('.childCheckBox:checked').length === $(this).closest('.content').find('.childCheckBox').length
);
});
//clicking the parent checkbox should check or uncheck all child checkboxes
$(".parentCheckBox").click(
function() {
$(this).parents('fieldset:eq(0)').find('.childCheckBox').prop('checked', this.checked);
}
);
//clicking the last unchecked or checked checkbox should check or uncheck the parent checkbox
$('.childCheckBox').click(
function() {
if ($(this).parents('fieldset:eq(0)').find('.parentCheckBox').attr('checked') == true && this.checked == false)
$(this).parents('fieldset:eq(0)').find('.parentCheckBox').attr('checked', false);
if (this.checked == true) {
var flag = true;
$(this).parents('fieldset:eq(0)').find('.childCheckBox').each(
function() {
if (this.checked == false)
flag = false;
}
);
$(this).parents('fieldset:eq(0)').find('.parentCheckBox').attr('checked', flag);
}
}
);
}
);
$('fieldset').each(function(){
var $childCheckboxes = $(this).find('input.childCheckBox'),
no_checked = $childCheckboxes.filter(':checked').length;
if($childCheckboxes.length == no_checked){
$(this).find('.parentCheckBox').prop('checked',true);
}
});
实际上非常简单,只需遍历每个字段集,检查检查的长度与总的长度,并使用.prop()
更改父字段的检查属性
小提琴 http://jsfiddle.net/3vker/1/
相关文章:
- 选中了基于图像的复选框,但未将值添加到文本字段中
- Firefox:点击并更改未附加到文档树中的复选框元素上的事件
- 在同一行中为每个复选框显示和隐藏文本字段
- JavaScript-使用sessionStorage保存文档之间共享的复选框状态
- 如何使用选中的复选框文本自动填充输入文本字段
- 如果复选框未选中且文本字段不为空,则显示警报
- 根据文本字段中的用户输入隐藏/显示复选框
- 组合复选框以激活文本字段和有限数量的允许复选框
- 如果选中所有子复选框,如何选中父复选框 - 在文档就绪时
- 在“文档.表单 1.复选框.选中 == 真”
- 将 HTML 复选框值放入文本字段
- 如何在 Yii javascript 中使用复选框隐藏或显示文本字段
- 选中复选框时启用文本字段
- 如何在点击复选框时检索现有的jquery数据表(在文档上初始化)
- 如何在PDF文档中添加javascript来计算复选框的数量,并在文本框区域中输入值
- 显示在文档本身中选中了多少个表单复选框
- 如果我们在鼠标下移时设置preventDefault,然后双击文档,那么它就会阻止复选框/单选标签的默认行为
- 通过模态复选框选择将一个文档添加到另一个文档
- 使用文档计算复选框数组长度.getElementById在javascript
- 添加文档加载时无法检测复选框更改