Jquery - checked 全选与取消全选

 

html:

<!DOCTYPE html>
<html>
<head>
    <title>节点列表</title>
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/Css/Public.css">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/Css/node.css">
    <script type="text/javascript" src="__PUBLIC__/Js/jquery.js"></script>
    <script type="text/javascript" src="__PUBLIC__/Js/node.js"></script>
</head>
<body>
    <div id="wrap">
        <div>
            <a href="<{:U('/Admin/Rbac/role')}>" class="add-app">返回</a>
        </div>
        <div>
            <foreach name="node" item="app">
                <div class="app">
                    <p>
                        <strong><{$app.title}></strong>
                        <input type="checkbox" name="access[]"  value="<{$app.id}>_1" level='1' />
                    </p>

                    <foreach name="app.child" item="action">
                        <dl>
                            <dt>
                                <strong><{$action.title}></strong>
                                <input type="checkbox" name="access[]" value="<{$action.id}>_3" level='2'  />
                            </dt>

                            <foreach name="action.child" item="func">
                                <dd>
                                    <span><{$func.title}></span>
                                    <input type="checkbox" name="access[]" value="<{$func.id}>_3" level='3'  />
                                </dd>
                            </foreach>
                        </dl>
                    </foreach>
                </div>
            </foreach>
        </div>
    </div>
</body>
</html>

 

JS:

$(document).ready(function(){
    $('input[level="1"]').click(function() {            
        var inputs = $(this).parents('.app').find('input');
        $(this).prop('checked') ? inputs.prop('checked', true) : inputs.removeAttr('checked', false);
    });

    $('input[level="2"]').click(function() {
        var inputs = $(this).parents('dl').find('input');
        $(this).prop('checked') ? inputs.prop('checked', true) : inputs.removeAttr('checked', false);
    });
});

 

posted on 2016-02-12 16:36  ultrastrong  阅读(306)  评论(0编辑  收藏  举报