JQuery复选框全选效果如何实现


Posted in jQuery onMay 08, 2020

Js相关技术

checked属性

如何获取所有复选框:document.getElementsByName、

需求分析

​ 在我们对表格处理的时,有些情况下,我们需要对表格进行批量处理

技术分析

第一种方法:选择器[属性名称='属性值']

$("input[type='checkbox']:gt(0)").prop("checked",this.checked);

第二种方法:使用层级选择器来实现 tbody > tr > td > input

$("tbody > tr > td > input").prop("checked",this.checked);

第三种方法:

JQuery复选框全选效果如何实现

// #tab > tbody > tr:nth-child(4) > td:nth-child(1) > input[type="checkbox"]

代码实现

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="../js/jquery-1.11.0.js" ></script>
		
		<script>
						
			/*
			 表格全选和全不选
			 进一步确定事件: 点击事件
			 
			 */
			$(function(){
				//绑定点击事件
				//this 代表的是当前函数的所有者
				$("#checkAll").click(function(){
					//获取当前选中状态
//					alert(this.checked);
					//获取所有分类项的checkbox
					// 选择器[属性名称='属性值']
//					$("input[type='checkbox']:gt(0)").prop("checked",this.checked);
					
					//使用层级选择器来实现 tbody > tr > td > input
				//	$("tbody > tr > td > input").prop("checked",this.checked); //这个可行

				//	#tab > tbody > tr:nth-child(4) > td:nth-child(1) > input[type="checkbox"]
					$("input").prop("checked",this.checked);
					
				});
			});
			
		</script>
	</head>
	<body>
		<table border="1px" width="600px" id="tab">
			<thead>
				<tr >
					<td>
						<input type="checkbox" id="checkAll" />
					</td>
					<td>分类ID</td>
					<td>分类名称</td>
					<td>分类商品</td>
					<td>分类描述</td>
					<td>操作</td>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>
						<input type="checkbox" />
					</td>
				<td>1</td>
				<td>手机数码</td>
				<td>华为,小米,尼康</td>
				<td>黑马数码产品质量最好</td>
				<td>
					<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a>|<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a>
				</td>
			</tr>
			<tr>
				<td>
					<input type="checkbox" />
				</td>
				<td>2</td>
				<td>成人用品</td>
				<td>充气的</td>
				<td>这里面的充气电动硅胶的</td>
				<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a>|<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a></td>
			</tr>
			<tr>
				<td>
					<input type="checkbox" />
				</td>
				<td>3</td>
				<td>电脑办公</td>
				<td>联想,小米</td>
				<td>笔记本特卖</td>
				<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a>|<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a></td>
			</tr>
			<tr>
				<td>
					<input type="checkbox" />
				</td>
				<td>4</td>
				<td>馋嘴零食</td>
				<td>辣条,麻花,黄瓜</td>
				<td>年货</td>
				<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a>|<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a></td>
			</tr>
			<tr>
				<td>
					<input type="checkbox" />
				</td>
				<td>5</td>
				<td>床上用品</td>
				<td>床单,被套,四件套</td>
				<td>都是套子</td>
				<td><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a>|<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >删除</a></td>
			</tr>
			</tbody>
		</table>
		
	</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jquery实现提示语淡入效果
May 05 jQuery
最常用的jQuery表单验证(简单)
May 23 jQuery
jQuery复合事件结合toggle()方法的用法示例
Jun 10 jQuery
jQuery 表单序列化实例代码
Jun 11 jQuery
jQuery简介_动力节点Java学院整理
Jul 04 jQuery
jquery 一键复制到剪切板的实例
Sep 20 jQuery
JQuery 选择器、DOM节点操作练习实例
Sep 28 jQuery
JQuery选中select组件被选中的值方法
Mar 08 jQuery
详解使用jQuery.i18n.properties实现js国际化
May 04 jQuery
jquery实现图片无缝滚动 蒙版遮蔽效果
Jan 11 jQuery
jQuery实现聊天对话框
Feb 08 jQuery
jquery+css3实现的经典弹出层效果示例
May 16 jQuery
JQuery省市联动效果实现过程详解
May 08 #jQuery
jquery实现手风琴案例
May 04 #jQuery
jQuery实现的移动端图片缩放功能组件示例
May 01 #jQuery
jQuery实现移动端图片上传预览组件的方法分析
May 01 #jQuery
jQuery实现的上拉刷新功能组件示例
May 01 #jQuery
jQuery实现的解析本地 XML 文档操作示例
Apr 30 #jQuery
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
Apr 30 #jQuery
You might like
php在字符串中查找另一个字符串
2008/11/19 PHP
php 操作符与控制结构
2012/03/07 PHP
PHP实现加强版加密解密类实例
2015/07/29 PHP
Yii 框架使用数据库(databases)的方法示例
2020/05/19 PHP
Javascript 验证上传图片大小[客户端]
2009/08/01 Javascript
jquery tablesorter.js 支持中文表格排序改进
2009/12/09 Javascript
js下将字符串当函数执行的方法
2011/07/13 Javascript
Jquery Ajax xmlhttp请求成功问题
2015/02/04 Javascript
JS实现点击文字对应DIV层不停闪动效果的方法
2015/03/02 Javascript
浅谈javascript获取元素transform参数
2015/07/24 Javascript
JS中多种方式创建对象详解
2016/03/22 Javascript
微信小程序 教程之数据绑定
2016/10/18 Javascript
使用jquery实现的循环连续可停顿滚动实例
2016/11/23 Javascript
浅谈Node.js:fs文件系统模块
2016/12/08 Javascript
JS实现物体带缓冲的间歇运动效果示例
2016/12/22 Javascript
jQuery实现对象转为url参数的方法
2017/01/11 Javascript
AngularJS双向绑定和依赖反转实例详解
2017/04/15 Javascript
vue实现拖拽效果
2019/12/23 Javascript
在vue中动态修改css其中一个属性值操作
2020/12/07 Vue.js
python读取注册表中值的方法
2013/04/08 Python
python中字符串内置函数的用法总结
2018/09/13 Python
解决jupyter notebook显示不全出现框框或者乱码问题
2020/04/09 Python
全球速卖通西班牙站:AliExpress西班牙
2017/10/30 全球购物
英语翻译系毕业生求职信
2013/09/29 职场文书
简历中自我评价范文3则
2013/12/14 职场文书
网上蛋糕店创业计划书
2014/01/24 职场文书
优秀社区干部事迹材料
2014/02/03 职场文书
竞争性谈判邀请书
2014/02/06 职场文书
会计学毕业生求职信
2014/06/25 职场文书
药品营销专业毕业生自荐信
2014/07/02 职场文书
私人房屋买卖协议书
2014/10/04 职场文书
政协工作总结2015
2015/05/20 职场文书
启动Tomcat时出现大量乱码的解决方法
2021/06/21 Java/Android
Python可视化学习之matplotlib内置单颜色
2022/02/24 Python
「偶像大师 MILLION LIVE!」七尾百合子手办开订
2022/03/21 日漫
VUE递归树形实现多级列表
2022/07/15 Vue.js