如果选中所有子复选框,如何检查父复选框 - 文档准备好了吗?

时间:2013-10-29 21:05:06

标签: javascript php jquery checkbox

我有几组复选框,就像在这个jsFiddle中一样工作:

http://jsfiddle.net/3vker/

如果选中了所有子复选框,则基本上会选中父复选框,如果未选中任何子复选框,则取消选中父复选框。

除了一个问题外,它的效果很好:

  • 页面是交互式的,因此可以检查所有子复选框(通过PHP中的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);
                }
            }
        );
    }
); 

1 个答案:

答案 0 :(得分:4)

$('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/

相关问题