这里是我初步学习jquery后中巨做的一个关于复选框的小功能:

    点击右边选项如果勾上,对应的左边三个小项全部选中,反之全不选,

左边只要有一个没选中,右边大项就取消选中,反之左边全部选中的话,左边大项即为选中,下面是样式图。

<div class="box">
        <table id="table1" class="mytable">
            <tr>
                <td>
                    <span>
                        <input type="checkbox" id="chkPromote" class="parentfunc" />图书管理
                    </span>
                </td>
                <td>
                    <span>
                        <input type="checkbox" id="Checkbox1" />新增图书管理
                    </span> <span>
                        <input type="checkbox" id="Checkbox2" />修改图书管理
                    </span> <span>
                        <input type="checkbox" id="Checkbox3" />删除图书管理
                    </span>
                </td>
            </tr>
            <tr>
                <td>
                    <span>
                        <input type="checkbox" id="Checkbox4" class="parentfunc" />会员管理
                    </span>
                </td>
                <td>
                    <span>
                        <input type="checkbox" id="Checkbox5" />新增会员管理
                    </span> <span>
                        <input type="checkbox" id="Checkbox6" />修改会员管理
                    </span> <span>
                        <input type="checkbox" id="Checkbox7" />删除会员管理
                    </span>
                </td>
            </tr>
            <tr>
                <td>
                    <span>
                        <input type="checkbox" id="Checkbox8" class="parentfunc" />系统设置
                    </span>
                </td>
                <td>
                    <span>
                        <input type="checkbox" id="Checkbox9" />管理员设置
                    </span> <span>
                        <input type="checkbox" id="Checkbox10" />角色管理
                    </span> <span>
                        <input type="checkbox" id="Checkbox11" />权限管理
                    </span>
                </td>
            </tr>

</table>

</div>

jQuery代码如下:

<script type="text/javascript">

  //页面加载
        $(function () {
            //点任意大项时
            $("tr td:first-child span input").click(function () {
     //利用点击的的大项的位置找到它所在的同级td下的所有小项设置他们的checked(大项是选中那就全部选中,反之、、、就能简单实现全选和全部选的功能)
                $(this).parents("td").siblings().find("input[type=checkbox]").prop("checked",$(this).prop("checked"));
            })

//点任意小项时
            $("tr td:last-child span input").click(function () {
                var a = 0;

    //循环点击当前小项的同级的所有小项,所有的小项的选中情况
                for (var i = 0; i < $(this).parents("td").children.length + 1 ; i++) {
      //判断如果哪怕找到一个小项为没有选中,那么将对应的大项设为不选中。
                    if ($(this).parents("td").children(":eq(" + i + ")").children().prop("checked") == false) {
        //设大项为不选中
                        $(this).parents("td").siblings().find("input[class=parentfunc]").prop("checked", false);

      //只要有一个小项为false那么就a+1
                        a++;
                    }
                }

    //判断这个a变量,如果等等于0就说明所有小项都选中了,那么就把对应的大项选中
                if (a == 0) {
      //选中大项
                    $(this).parents("td").siblings().find("input[class=parentfunc]").prop("checked", true);
                }
            })
        })
    </script>

  

jQuery关于复选框的基本小功能的更多相关文章

  1. 对jquery操作复选框

    摘要:jquery操作复选框.使用更简洁易懂,思路清晰,逻辑更明了,很实用 <!DOCTYPE html> <html> <head> <meta chars ...

  2. jquery操作复选框(checkbox)十二技巧

    jquery操作复选框(checkbox)的12个小技巧. 1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$( ...

  3. jQuery实现复选框的全选、反选、并且根据复选框的<checked属性>控制多个对应div的显示/隐藏

    <!doctype html><html> <head> <meta charset="utf-8"> <title>j ...

  4. jQuery取复选框值、下拉列表里面的属性值、取单选按钮的属性值、全选按钮、JSON存储、*去空格

    1.jquery取复选框的值<!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></scri ...

  5. jquery判断复选框checkbox是否被选中

    jquery判断复选框checkbox是否被选中 使用is方法 //如果选中返回true //如果未选中返回false .is(':checked');

  6. jquery判断复选框是否选中

    jquery判断复选框是否被选中 $(function(){ $(document).on("click", ".checkbox",function(){ v ...

  7. jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等

    转载:https://blog.csdn.net/chenchunlin526/article/details/77448168 jQuery操作复选框checkbox技巧总结 --- 设置选中.取消 ...

  8. jQuery判断复选框checkbox的选中状态

    通过jQuery设置复选框为选中状态 复选框 <input type="checkbox"/> 错误代码: $("input").attr(&quo ...

  9. jquery实现复选框全选,全不选,反选中的问题

    今天试了一下用jquery选择复选框,本来以为很简单的东西却有bug,于是搜索了一下找到了解决方法. html代码如下(这里没有用任何样式,就没有再放css了): <html> <h ...

随机推荐

  1. android之键盘转载

    显示键盘: EditText editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.request ...

  2. JS获取屏幕分辨率以及当前对象大小等

    <script type="text/javascript"> function getInfo(){ var s = ""; s += " ...

  3. C#常用排序和查找算法

    1.C#堆排序代码 private static void Adjust (int[] list, int i, int m) { int Temp = list[i]; int j = i * 2 ...

  4. 三、docker学习笔记——安装postgresql

    1.docker pull postgres 2.docker run --name postgres -e POSTGRES_PASSWORD=123456 -p 5432:5432 -d post ...

  5. 【Leetcode】【Medium】Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. Hadoop学习---Hadoop的MapReduce的原理

    MapReduce的原理 MapReduce的原理 NameNode:存放文件的元数据信息 DataNode:存放文件的具体内容 ResourceManager:资源管理,管理内存.CPU等 Node ...

  7. zan扩展安装

    官方地址 https://github.com/youzan/zan //提示缺少libcurl扩展时候安装 yum install libcurl-devel //安装完zan.so php -m提 ...

  8. Oracle 内存使用建议性能视图

    下面三个查询结果均可查询出随着内存参数设置的变化性能的变化情况,对oracle数据库内存的设置有一定的建议和指导作用. select t.SGA_SIZE,t.ESTD_DB_TIME_FACTOR ...

  9. Perl中的字符串操作函数

    1.$position = index(string,substring,skipchars): 该函数返回子串substring在字符串string中的位置,如果不存在,则返回-1:参数skipch ...

  10. JS中的prototype (转载)

    JS中的prototype   JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是 ...