JQuery实现全选、反选和取消功能

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>JQ实现正、反选</title>
 6 </head>
 7 <body>
 8     <table  border="1px" style="width: 200px;margin-bottom: 10px">
 9         <thead>
10             <tr>
11                 <th>#</th>
12                 <th>姓名</th>
13                 <th>性别</th>
14             </tr>
15         </thead>
16         <tbody>
17             <tr>
18                 <td><input type="checkbox"></td>
19                 <td>Alex</td>
20                 <td>女</td>
21             </tr>
22             <tr>
23                 <td><input type="checkbox"></td>
24                 <td>Egon</td>
25                 <td>女</td>
26             </tr>
27             <tr>
28                 <td><input type="checkbox"></td>
29                 <td>Qimi</td>
30                 <td>女</td>
31             </tr>
32         </tbody>
33     </table>
34     <input type="button"  value="全选" id="i1" class="c1">
35     <input type="button" value="反选" id="i2" class="c1">
36     <input type="button" value="取消" id="i3">
37     <script src="jquery-3.2.1.js"></script>
38     <script>
39         // <!-----------------------------------全选------------------------------------>
40         var $in_1 = $("#i1");
41         //用第一种循环的方式全部选中,each的循环体不用加索引取值
42         // $in_1.on("click",function () {
43         //     var $cheele = $(":checkbox");
44         //     $cheele.each(function () {
45         //         //为input标签增加固有属性checked
46         //         $(this).prop("checked",true);
47         //     })
48         // });
49         //用第二种循环的方式全部选中
50         // $in_1.click("click",function () {
51         //     var $cheele = $(":checkbox");
52         //     $.each($cheele,function () {
53         //         $(this).prop("checked",true);
54         //     })
55         // });
56         //另一种全选的方法
57             //要执行的语句不能直接你跟在","之后!!!
58         $in_1.on("click",function () {
59             $(":checkbox").prop("checked",true);
60         });
61         //-----------------------------------------取消--------------------------------------------------
62         var $in_2 = $("#i3");
63         $in_2.on("click",function () {
64             $(":checkbox").prop("checked",false);
65         });
66         //-----------------------------------------反选--------------------------------------------------
67         var $in_3 = $("#i2");
68         $in_3.on("click",function () {
69             $(":checkbox").each(function () {
70                 $(this).prop("checked",!$(this).prop("checked"));
71             })
72         });
73     </script>
74 </body>
75 </html>

转载于:https://www.cnblogs.com/liuyinzhou/p/8179021.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值