Jquery检查嵌套ul li中的父复选框

Pie*_*ugh 8 html javascript jquery

我有一个脚本,它将检查并取消选中嵌套列表中的所有子复选框.我现在正试图得到它所以我可以检查一个低级复选框,它将检查所有父母只回到最高级别.这是一个JSFiddle

<ul class="tree" id="tree">

    <li><input type="checkbox" name="account_settings" value="yes">Account Settings <!-- AND SHOULD CHECK HERE -->
        <ul>
            <li><input type="checkbox" name="one" value="one">AS One</li>
            <li><input type="checkbox" name="two" value="two">AS Two</li>
            <li><input type="checkbox" name="user_roles" value="user_roles">Users &amp; Roles <!-- SHOULD CHECK HERE -->
                <ul>
                    <li><input type="checkbox" name="user_role" value="add">Add</li>
                    <li><input type="checkbox" name="user_role" value="delete">Delete</li> <!-- CHECK HERE -->
                </ul>
            </li>
        </ul>
    </li>

    <li><input type="checkbox" name="rl_module" value="yes">RL Module</li>

    <li><input type="checkbox" name="rl_module" value="yes">Accounting
        <ul>
            <li><input type="checkbox" name="vat" value="yes">VAT</li>
            <li><input type="checkbox" name="bank_account" value="yes">Banking
                <ul>
                    <li><input type="checkbox" name="view" value="yes">View</li>
                    <li><input type="checkbox" name="crud" value="yes">CRUD</li>
                </ul>
            </li>
        </ul>
    </li>

</ul>
Run Code Online (Sandbox Code Playgroud)

和相应的javascript:

$('input[type=checkbox]').click(function(){

    // if is checked
    if($(this).is(':checked')){

        // check all children
        $(this).parent().find('li input[type=checkbox]').prop('checked', true);

        // check all parents
        $(this).parent().prev().prop('checked', true);

    } else {

        // uncheck all children
        $(this).parent().find('li input[type=checkbox]').prop('checked', false);

    }

});
Run Code Online (Sandbox Code Playgroud)

ᾠῗᵲ*_*ᵲᄐᶌ 11

看起来你想要这样的东西

$('input[type=checkbox]').click(function(){
    if(this.checked){ // if checked - check all parent checkboxes
        $(this).parents('li').children('input[type=checkbox]').prop('checked',true);
    }
    // children checkboxes depend on current checkbox
    $(this).parent().find('input[type=checkbox]').prop('checked',this.checked); 
});
Run Code Online (Sandbox Code Playgroud)

FIDDLE

如果你想检查上下层次结构 - 你可以这样做

$('input[type=checkbox]').click(function(){
    // children checkboxes depend on current checkbox
    $(this).next().find('input[type=checkbox]').prop('checked',this.checked);
    // go up the hierarchy - and check/uncheck depending on number of children checked/unchecked
    $(this).parents('ul').prev('input[type=checkbox]').prop('checked',function(){
        return $(this).next().find(':checked').length;
    });
});
Run Code Online (Sandbox Code Playgroud)

FIDDLE

  • 这是最简洁的解决方案,可以完全正常运行。 (2认同)

j08*_*691 6

这应该这样做:

$('input[type=checkbox]').click(function () {
    $(this).parent().find('li input[type=checkbox]').prop('checked', $(this).is(':checked'));
    var sibs = false;
    $(this).closest('ul').children('li').each(function () {
        if($('input[type=checkbox]', this).is(':checked')) sibs=true;
    })
    $(this).parents('ul').prev().prop('checked', sibs);
});
Run Code Online (Sandbox Code Playgroud)

jsFiddle例子

最新更新处理层次结构和兄弟姐妹的上下.

  • 这样的事情是你在看每个级别的兄弟姐妹时如何处理未经检查的父母:http://jsfiddle.net/Z82FR/3/ (3认同)

Loc*_*age 2

只需使用jquery.parents(). 它与 find() 有点相似,只不过它搜索所有父级。像这样的东西可能接近您正在寻找的东西:

$(this).parents('li').each(function() {
  $(this).children('input').prop('checked', true);
});
Run Code Online (Sandbox Code Playgroud)

请参阅http://api.jquery.com/parents/了解更多信息。

编辑:好的,这是一个有效的解决方案:

http://jsfiddle.net/3y3Pb/12/

EDIT2:这里有一个更简化的解决方案:

http://jsfiddle.net/3y3Pb/14/