简体   繁体   English

自动检查父复选框

[英]auto check parent checkbox

I have an AJAX request that pulls in a list of folders with a checkbox next to each list item. 我有一个AJAX请求,它带有一个文件夹列表,每个列表项旁边都有一个复选框。 If I check the checkbox next to a parent folder, I have all of the children folders automatically check by doing this: 如果我选中父文件夹旁边的复选框,我会自动检查所有子文件夹:

var checkChildCheckBoxes = function(){
            var isAlreadyChecked = $(this).attr('isChecked');
            if(isAlreadyChecked == 'true'){
                $(this).parents(':eq(1)').find('.userPermissionCheckBox').each(function(){
                    $(this).prop('checked', false);
                });
                $(this).attr('isChecked', 'false');
            }
            else{   
                $(this).parents(':eq(1)').find('.userPermissionCheckBox').each(function(){
                    $(this).prop('checked', true);
                });
                $(this).attr('isChecked', 'true');
            }
        }

This works splendidly. 这非常有效。 What I'm trying to solve is if you DID NOT have the parent checkbox checked and you check a child checkbox, it will automatically check the parent. 我想要解决的是,如果您没有选中父复选框,并且您选中了一个子复选框,它将自动检查父级。

I tried with no success to do it like this: 我尝试这样做没有成功:

if($(this).parent('ul').find('.userPermissionCheckBox:eq(0)').is('checked')){

                }
                else{
                    $(this).parent('ul').find('.userPermissionCheckBox:eq(0)').prop('checked', true);
                }

I didn't want to load this question with code, so I made a fiddle displaying how the list is structured and the way I'm using it. 我不想用代码加载这个问题,所以我做了一个小提琴,显示了列表的结构以及我使用它的方式。 Go here: http://jsfiddle.net/BTXNk/1/ I'm kind of a n00b with Javascript, hope this question isn't stupid. 转到这里: http//jsfiddle.net/BTXNk/1/我是一个使用Javascript的n00b,希望这个问题不是愚蠢的。 Thank you for taking the time to read it. 感谢您抽出宝贵时间阅读它。 http://jsfiddle.net/BTXNk/1/ http://jsfiddle.net/BTXNk/1/

I think there's a bit of logic issue here. 我认为这里存在一些逻辑问题。

Say you have the parent, a child and a child's child, per your requirement, when you click on an unchecked child, the current element is checked and the child element of this as well as the parent of this is checked as well, but the sibling of this is not checked right? 假设您有父母,孩子和孩子的孩子,根据您的要求,当您点击未经检查的孩子时,会检查当前元素,并检查此元素及其父元素,但是这个兄弟姐妹没被检查对吗?

What happens when you click on the parent? 单击父级会发生什么? None of the check box is checked? 没有勾选复选框? So if I decided to check all the check box, but I already have a child check box check, that means I will have to check the parent check box to un-check everything and then click on it again to check all the check boxes? 因此,如果我决定选中所有复选框,但我已经有一个子复选框检查,这意味着我必须检查父复选框以取消检查所有内容,然后再次单击它以检查所有复选框?

This is not a very good user experiences. 这不是一个非常好的用户体验。 Anyway, I think the best thing to do is separate the function click events out into three different listeners instead of trying to do it in one function. 无论如何,我认为最好的办法是将函数单击事件分成三个不同的侦听器,而不是尝试在一个函数中执行它。 With that in mind, I didn't write out the use case when you have everything checked and you un-check one of the child, then the parent check box should still be checked. 考虑到这一点,当您检查完所有内容并且取消检查其中一个子项时,我没有写出用例,那么仍应检查父复选框。 You'll have to keep expanding these. 你必须继续扩展它们。

<ul class="level-one">
    <li class="level-one-closed">
        <span class="level-one-folder">
            <input type="checkbox" class="userPermissionCheckBox-level-one" />
            Parent
        </span>
        <ul class="level-two">
            <li class="level-two-closed">
                <span class="level-two-folder">
                    <input type="checkbox" class="userPermissionCheckBox-level-two" />
                    Child
                </span>
                <ul class="level-three">
                    <li>
                        <span class="level-three-folder">
                            <input type="checkbox" class="userPermissionCheckBox-level-three" />
                            Child's Child
                            </span>
                    </li>
                </ul>
            </li>
            <li class="level-two-closed">
                <span class="level-two-folder">
                    <input type="checkbox" class="userPermissionCheckBox-level-two" />
                    Child
                </span>
                <ul class="level-three">
                    <li>
                        <span class="level-three-folder">
                            <input type="checkbox" class="userPermissionCheckBox-level-three" />
                            Child's Child
                        </span>
                    </li>
                    <li>
                        <span class="level-three-folder">
                            <input type="checkbox" class="userPermissionCheckBox-level-three" />
                            Child's Child
                        </span>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

var $levelOneCheck = $('.userPermissionCheckBox-level-one');
var $levelTwoCheck = $('.userPermissionCheckBox-level-two');
var $levelThreeCheck = $('.userPermissionCheckBox-level-three');

$levelOneCheck.click(function() {
    var $isChecked = $(this).attr('isChecked');
    if ($isChecked === 'true') {
        $(this).attr('isChecked', 'false');
        $levelTwoCheck.prop('checked', false).attr('isChecked', 'false');
        $levelThreeCheck.prop('checked', false).attr('isChecked', 'false');
    } else {
        $(this).attr('isChecked', 'true');
        $levelTwoCheck.prop('checked', true).attr('isChecked', 'true');
        $levelThreeCheck.prop('checked', true).attr('isChecked', 'true');
    }
});

$levelTwoCheck.click(function() {
    var $isCheckedLevelTwo = $(this).attr('isChecked');
    if ($isCheckedLevelTwo === 'true') {
        $(this).attr('isChecked', 'false');
        $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', false).attr('isChecked', 'false');
        $(this).closest('.level-two-closed').find('.level-three-folder .userPermissionCheckBox-level-three').prop('checked', false).attr('isChecked', 'false');

    } else {
        $(this).attr('isChecked', 'true');
        $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', true).attr('isChecked', 'true');
        $(this).closest('.level-two-closed').find('.level-three-folder .userPermissionCheckBox-level-three').prop('checked', true).attr('isChecked', 'true');
    }
});

$levelThreeCheck.click(function() {
    var $isCheckedLevelTwo = $(this).attr('isChecked');
    if ($isCheckedLevelTwo === 'true') {
        $(this).attr('isChecked', 'false');
        $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', false).attr('isChecked', 'false');
        $(this).closest('.level-two-closed').find('.level-two-folder .userPermissionCheckBox-level-two').prop('checked', false).attr('isChecked', 'false');

    } else {
        $(this).attr('isChecked', 'true');
        $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', true).attr('isChecked', 'true');
        $(this).closest('.level-two-closed').find('.level-two-folder .userPermissionCheckBox-level-two').prop('checked', true).attr('isChecked', 'true');
    }
});

http://jsfiddle.net/xzigraz/BTXNk/3/ http://jsfiddle.net/xzigraz/BTXNk/3/

As Matt said , you need a three-state checkbox, with a state for when some sub-items are checked and some are not. 正如Matt所说 ,你需要一个三态复选框,其中包含一些状态,用于检查某些子项,而另一些则不是。

You can do a three-state checkbox by making “indeterminate” the third state. 您可以通过使“不确定”成为第三个状态来执行三态复选框。 You can set a checkbox to “indeterminate” with $(this).prop("indeterminate", true); 您可以使用$(this).prop("indeterminate", true);将复选框设置为“indeterminate” $(this).prop("indeterminate", true); . The checkbox will then look something like this: 该复选框将如下所示: 不确定的复选框 .

See this article on indeterminate checkboxes . 请参阅有关不确定复选框的文章 That article not only explains indeterminate checkboxes, but has a demo of exactly what you want in the “Use Case?” section in the middle – though the article says that demo is incomplete because it only checks one level up. 那篇文章不仅解释了不确定的复选框,而且还在中间的“使用案例?”部分中准确演示了你想要的内容 - 虽然文章说演示是不完整的,因为它只检查一个级别。 Here is the code that demo uses: 以下是演示使用的代码:

$(function() {
    // Apparently click is better chan change? Cuz IE?
    $('input[type="checkbox"]').change(function(e) {
        var checked = $(this).prop("checked"),
        container = $(this).parent(),
        siblings = container.siblings();

        container.find('input[type="checkbox"]').prop({
            indeterminate: false,
            checked: checked
        });

        function checkSiblings(el) {
            var parent = el.parent().parent(),
            all = true;

            el.siblings().each(function() {
                return all = ($(this).children('input[type="checkbox"]').prop("checked") === checked);
            });

            if (all && checked) {
                parent.children('input[type="checkbox"]').prop({
                    indeterminate: false,
                    checked: checked
                });
                checkSiblings(parent);
            } else if (all && !checked) {
                parent.children('input[type="checkbox"]').prop("checked", checked);
                parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0));
                checkSiblings(parent);
            } else {
                el.parents("li").children('input[type="checkbox"]').prop({
                    indeterminate: true,
                    checked: false
                });
            }
        }

        checkSiblings(container);
    });
});
$(document).ready(function () {
    $('input[type=checkbox]').click(function () {
        // if is checked
        if ($(this).is(':checked')) {
            $(this).parents('li').children('input[type=checkbox]').prop('checked', true);
            $(this).parent().find('li input[type=checkbox]').prop('checked', true);    
        } else {   
            $(this).parents('li').children('input[type=checkbox]').prop('checked', false);
            // uncheck all children
            $(this).parent().find('li input[type=checkbox]').prop('checked', false);  
        }
    });
});

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

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