复选框操作

js代码

<script type="text/javascript">
        //全选/全不选
        function ChangeRoleStatus(id) {
            var status = $("#" + id).attr("checked");
            if (status == true || status == "checked") {
                $("#" + id).attr("checked", "checked");
                $("#" + id + "1 tr").each(function () {
                    $(this).find("input:checkbox").attr("checked", "checked");
                })
                //$("#" + id + "1 input[type=checkbox]").attr('checked', "checked");
            }
            else {
                $("#" + id).removeAttr("checked");
                $("#" + id + "1 tr").each(function () {
                    $(this).find("input:checkbox").removeAttr("checked");
                })
                //                $("#" + id + "1").find("input:checkbox").removeAttr("checked");
            }
        }
        //当子项选中一个时,父项选中;当子项没有选中一个时,父项取消选中
        function ChangeChildStatus(id) {
            //            var strs = id.spli('e');
            var status = $("#the" + id).attr("checked");
            var pid = $("#the" + id).attr("name");

            if (status == true || status == "checked") {
                $("#the" + id).attr("checked", "checked");
                var num = 0;
                $("#" + pid + "1 tr").each(function () {
                    status = $(this).find("input:checkbox").attr("checked");
                    if (status == true || status == "checked") {
                        num += 1;
                    }
                })

                if (num == 1) {
                    $("#" + pid).attr("checked", "checked");
                }
            }
            else {
                $("#the" + id).removeAttr("checked");
                var num1 = 0;
                $("#" + pid + "1 tr").each(function () {
                    status = $(this).find("input:checkbox").attr("checked");
                    if (status != true && status != "checked") {
                        num1 += 1;
                    }
                });
                if (num1 == $("#" + pid + "1 tr").find("input:checkbox").length) {
                    $("#" + pid).removeAttr("checked");
                }

            }
            //var pid = $("#" + id).parent().parent().parent().parent().attr("id");
            //alert(pid);
        }
        function SaveRoleProview() { 
            
        }
    </script>
View Code

页面代码

<table class="table-bordered" style=" 50%">
                    <tr>
                        <th class="list_01">
                            <label>
                                <input type="checkbox" id="1" onclick="ChangeRoleStatus(1)" />会员管理</label>
                        </th>
                        <td>
                            <table id="11">
                                    <tr class="contorl_table_tr">
                                        
                                        <td>
                                            <label>
                                                <input type="checkbox" id="the1" name="1" onclick="ChangeChildStatus(1)" />会员列表</label>
                                        </td>
                                    </tr>
                                    <tr class="contorl_table_tr">
                                        
                                        <td>
                                            <label>
                                                <input type="checkbox" id="the2" name="1" onclick="ChangeChildStatus(2)" />会员登记</label>
                                        </td>
                                    </tr>
                                    <tr class="contorl_table_tr">
                                        
                                        <td>
                                            <label>
                                                <input type="checkbox" id="the3" name="1" onclick="ChangeChildStatus(3)" />会员等级</label>
                                        </td>
                                    </tr>
                                    <tr class="contorl_table_tr">
                                        
                                        <td>
                                            <label>
                                                <input type="checkbox" id="the62" name="1" onclick="ChangeChildStatus(62)" />会员退货</label>
                                        </td>
                                    </tr>
                                    <tr class="contorl_table_tr">
                                        
                                        <td>
                                            <label>
                                                <input type="checkbox" id="the65" name="1" onclick="ChangeChildStatus(65)" />会员充值</label>
                                        </td>
                                    </tr>
                            </table>
                        </td>
                    </tr>
                </table>
View Code
原文地址:https://www.cnblogs.com/xibianriluo/p/5489271.html