微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery checkbox的三级联动

 

 代码

<!--  * @Descripttion: your project  * @version: 1.0  * @Author: guohanting  * @Date: 2021-05-13 17:17:26  * @LastEditors: Please set LastEditors  * @LastEditTime: 2021-05-14 09:17:20 --> <!DOCTYPE html> <html lang="en">
<head>     <Meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />     <Meta charset="utf-8" />     <title>Blank Page - Ace Admin</title>     <script src="js/jquery-2.1.4.min.js"></script>
    <Meta name="description" content="" />     <Meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

    <style>         .listBox {             width: 100%;             border: 1px solid #ccc;             overflow: hidden;             margin-bottom: 5px;         }                  .listleft {             float: left;             border-right: 1px solid #ccc;             width: 200px;             padding: 5px 10px;         }                  .listright {             float: left;             width: auto;             padding: 5px 10px;         }     </style> </head>
<body class="no-skin">
    <!-- PAGE CONTENT BEGINS -->     <div class="page-header">         <h1> 表格 <small> <i class="ace-icon fa fa-angle-double-right"></i> checkBox 三级联动 </small> </h1>     </div>
    <table id="sample-table-1" class="table table-striped table-bordered table-hover dataTable">         <thead id="checkwrap">             <tr>                 <th class="center ">                     全选                     <label class="position-relative menu-all">                           <input type="checkBox" class="ace check1" />                         </label>                 </th>                 <th>                     <div class="listBox">                         <div class="listleft">                             亲属信息管理11                             <label class="position-relative">                                   <input type="checkBox" class="ace check2" name="node_ids[]"/>                                 </label>                         </div>                         <div class="listright">                             基本信息管理                             <label class="position-relative">                                   <input type="checkBox" class="ace check3" />                                 </label> 基本信息管理2                             <label class="position-relative">                                   <input type="checkBox" class="ace check3" />                                  </label> 基本信息管理3                             <label class="position-relative">                                   <input type="checkBox" class="ace check3" />                                 </label>                         </div>                     </div>                     <div class="listBox">                         <div class="listleft">                             亲属信息管理11                             <label class="position-relative">                                   <input type="checkBox" class="ace check2" name="node_ids[]"/>                                 </label>                         </div>                         <div class="listright">                             基本信息管理                             <label class="position-relative">                                   <input type="checkBox" class="ace check3" />                                 </label> 基本信息管理2                             <label class="position-relative">                                   <input type="checkBox" class="ace check3" />                                  </label> 基本信息管理3                             <label class="position-relative">                                   <input type="checkBox" class="ace check3" />                                 </label>                         </div>                     </div>                 </th>             </tr>         </thead>
    </table>
    <!-- PAGE CONTENT ENDS -->
    <!--[if !IE]> -->     <script type="text/javascript">         window.jQuery || document.write("<script src='../assets/js/jquery.min.js'>" + "<" + "/script>");     </script>
    <!-- <![endif]-->
    <!--[if IE]> <script type="text/javascript">  window.jQuery || document.write("<script src='../assets/js/jquery1x.min.js'>"+"<"+"/script>"); </script> <![endif]-->

    <script>         $(function() {             check3();             check2();             check1();         })
        function check1() {             $('.check1').click(function() {                 var ok = $(this).prop("checked");                 $(this).parents('tr').find("input[type='checkBox']").prop('checked', ok);             })         };
        function check2() {             $('.check2').click(function() {                 var ok = $(this).prop("checked");                 // $(this).parents('.listleft').siblings().                 $(this).parents('.listleft').siblings().andSelf().find('.check3').prop("checked", ok);                 var oCheck2 = $(this).parents('.listBox').siblings().andSelf().find(".check2");                 $(this).parents('tr').find('.check1').prop("checked", trueorfalse(oCheck2));
            })         }
        function check3() {             $('.check3').click(function() {                 var ockeck3 = $(this).parent().siblings().andSelf().find(".check3");                 $(this).parents('.listBox').find('.check2').prop("checked", trueorfalse(ockeck3));                 var oCheck1 = $(this).parents('.listBox').siblings().andSelf().find('.check2');                 $(this).parents('tr').find('.check1').prop("checked", trueorfalse(oCheck1))
            })         }
        // 找同级元素的checked状态         function trueorfalse(obj) {             var bool = false;             $(obj).each(function() {
                if ($(this).prop("checked")) {                     bool = true;                 }             })             return bool;         }     </script> </body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐