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 ...
随机推荐
- 基于Tag的Docker自动构建
构建规则 一旦您的Tag符合“release-v$version"的形式,将触发自动构建:1)若您有$version相关的Tag构建规则,则以$version的Tag规则帮您构建:2)若您没 ...
- Hadoop High Availability高可用
HDFS HA Namenode HA 详解 hadoop2.x 之后,Clouera 提出了 QJM/Qurom Journal Manager,这是一个基于 Paxos 算法(分布式一致性算法) ...
- svn merge error must be ancestrally related to,trunk merge branch报错
trunk merge branch的时候报错 xxx must be ancestrally related to xxx,这个报错的意思是两者不关联,所以需要去建立关联. [回顾背景] ...
- impala安装笔记(Ubuntu)
1.Override 1.With Impala, you can query data, whether stored in HDFS or Apache HBase – including SEL ...
- JSP的重定向有两种forward和sendRedirect
jsp:forward重定向 当index.jsp存放在tomcat服务器应用目录下时:D:\Tomcat 7.0\webapps\Spring_shizhan4ban_Chapter05\index ...
- Stage划分和Task最佳位置算法源码彻底解密
本课主题 Job Stage 划分算法解密 Task 最佳位置算法实现解密 引言 作业调度的划分算法以及 Task 的最佳计算位置的算法,因为 Stage 的划分是DAGScheduler 工作的核心 ...
- 第一周 day1 Python学习笔记
为什么要学习Python? Python擅长的领域 1. python2.x中不支持中文编码,默认编码格式为ASCII码,而python3.x中支持Unicode编码,支持中文,变量名可以为中文,如: ...
- July 17th 2017 Week 29th Monday
A heart is a heavy burden. 心,可是很重的. Follow your heart, but always take your brain with you. Easy to ...
- July 06th 2017 Week 27th Thursday
Knowledge is the antidote to fear. 知识可以解除恐惧. Fear always steps from unknown things. Once we know wha ...
- June 28th 2017 Week 26th Wednesday
Anger begins with folly, and ends in repentance. 愤怒以愚蠢开始,以后悔告终. Learn to control your temper, don't ...