如何用JQuery实现复选框全选功能的长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1423个文字,预计阅读时间需要6分钟。
使用JavaScript获取所有复选框的方法:
1. 使用`document.getElementsByName`方法: javascript var checkboxes=document.getElementsByName('checkboxName'); 其中`'checkboxName'`是复选框的名称属性。
2. 技术分析: - `document.getElementsByName`可以获取页面中所有具有指定名称的元素。 - 通过返回的NodeList,可以遍历所有匹配的复选框。
3. 在处理表格时,有时需要批量处理复选框: - 可以使用循环遍历NodeList中的每个元素。 - 对每个复选框执行所需的操作,如选中或取消选中。
4. 技术分析: - 使用`for`循环遍历NodeList。 - 使用`checkbox.checked`属性来获取或设置复选框的选中状态。
5. 第一种方法: javascript var checkboxes=document.getElementsByName('checkboxName'); for (var i=0; i 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);
第三种方法:
// #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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计1423个文字,预计阅读时间需要6分钟。
使用JavaScript获取所有复选框的方法:
1. 使用`document.getElementsByName`方法: javascript var checkboxes=document.getElementsByName('checkboxName'); 其中`'checkboxName'`是复选框的名称属性。
2. 技术分析: - `document.getElementsByName`可以获取页面中所有具有指定名称的元素。 - 通过返回的NodeList,可以遍历所有匹配的复选框。
3. 在处理表格时,有时需要批量处理复选框: - 可以使用循环遍历NodeList中的每个元素。 - 对每个复选框执行所需的操作,如选中或取消选中。
4. 技术分析: - 使用`for`循环遍历NodeList。 - 使用`checkbox.checked`属性来获取或设置复选框的选中状态。
5. 第一种方法: javascript var checkboxes=document.getElementsByName('checkboxName'); for (var i=0; i 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);
第三种方法:
// #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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

