jQuery关于复选框的基本小功能
这里是我初步学习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关于复选框的基本小功能的更多相关文章
- 对jquery操作复选框
摘要:jquery操作复选框.使用更简洁易懂,思路清晰,逻辑更明了,很实用 <!DOCTYPE html> <html> <head> <meta chars ...
- jquery操作复选框(checkbox)十二技巧
jquery操作复选框(checkbox)的12个小技巧. 1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$( ...
- jQuery实现复选框的全选、反选、并且根据复选框的<checked属性>控制多个对应div的显示/隐藏
<!doctype html><html> <head> <meta charset="utf-8"> <title>j ...
- jQuery取复选框值、下拉列表里面的属性值、取单选按钮的属性值、全选按钮、JSON存储、*去空格
1.jquery取复选框的值<!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></scri ...
- jquery判断复选框checkbox是否被选中
jquery判断复选框checkbox是否被选中 使用is方法 //如果选中返回true //如果未选中返回false .is(':checked');
- jquery判断复选框是否选中
jquery判断复选框是否被选中 $(function(){ $(document).on("click", ".checkbox",function(){ v ...
- jQuery操作复选框checkbox技巧总结 ---- 设置选中、取消选中、获取被选中的值、判断是否选中等
转载:https://blog.csdn.net/chenchunlin526/article/details/77448168 jQuery操作复选框checkbox技巧总结 --- 设置选中.取消 ...
- jQuery判断复选框checkbox的选中状态
通过jQuery设置复选框为选中状态 复选框 <input type="checkbox"/> 错误代码: $("input").attr(&quo ...
- jquery实现复选框全选,全不选,反选中的问题
今天试了一下用jquery选择复选框,本来以为很简单的东西却有bug,于是搜索了一下找到了解决方法. html代码如下(这里没有用任何样式,就没有再放css了): <html> <h ...
随机推荐
- 01_Nginx入门
[Nginx概述] Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-Like协议下发行.其特点是占有内存少,并发能力强,事实上Nginx ...
- 【Python】回文palindrome——利用字符串反转
回文 palindrome Python 字符串反转string[::-1] Slice notation "[a : b : c]" means "count in i ...
- html 颜色在线取色器
推荐一个链接 还不错 http://www.atool.org/colorpicker.php
- SSIS ->> Environment Variables
SQL Server Integration Services(SSIS) 在2012版本引入了Environment Variables这个新特性.它允许我们为一个环境创建出一套变量用于为项目内的包 ...
- Congestion Avoidance in TCP
Congestion Avoidance in TCP Consequence of lack of congestion control When a popular resource is sha ...
- ldconfig: /usr/lib/libpython2.6.so.1.0-gdb.py is not an ELF file - it has the wrong magic bytes at the start.
https://bugzilla.redhat.com/show_bug.cgi?id=562980
- 沉淀,再出发:Maven的使用和规范
沉淀,再出发:Maven的使用和规范 一.前言 Maven作为项目管理工具,在一个大型项目开发的每个阶段都有着很大的用处,为什么需要这个东西呢,还是为了消除不确定性,统一化管理,正如我们做的每一件事其 ...
- 洛谷 P3175 [HAOI2015]按位或
题目分析 与hdu4336 Card Collector相似,使用min-max容斥. 设\(\max(S)\)表示集合\(S\)中最后一位出现的期望时间. 设\(\min(S)\)表示集合\(S\) ...
- UVa 11181 - Probability|Given(条件概率)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 1442 - Cave
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...