jQuery练习6--全选反选和全反选

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3   <head>
 4     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 5     <script type="text/javascript" src="../js/jquery-1.9.1.js"></script>
 6   </head>
 7   <body>
 8     <table border="1" align="center">
 9         <tr>
10             <th>状态</th>
11             <th>用户名</th>
12         </tr>
13         <tr>
14             <td><input type="checkbox"/></td>
15             <td></td>
16         </tr>
17         <tr>
18             <td><input type="checkbox"/></td>
19             <td></td>
20         </tr>        
21         <tr>
22             <td><input type="checkbox"/></td>
23             <td></td>
24         </tr>    
25         <tr>
26             <td><input type="checkbox"/></td>
27             <td></td>
28         </tr>
29         <tr>
30             <td><input type="checkbox"/></td>
31             <td></td>
32         </tr>    
33         <tr>
34             <td><input type="button" value="全选中"/></td>
35             <td><input type="button" value="全取消"/></td>
36             <td><input type="button" value="全反选"/></td>
37         </tr>        
38     </table>
39     <script type="text/javascript">
40         
41             //全取消失效
42               $(":button:eq(1)").attr("disabled","disabled");
43         
44             //1、全选中
45             var $button1 =  $("input[value='全选中']").first();
46             $button1.click(function(){
47                 
48                 $("input:checkbox").attr("checked","checked");
49                 //全选中按钮失效
50                 $(this).attr("disabled","disabled");
51                 //全取消生效
52                  $button2.removeAttr("disabled");
53             });
54             
55             //2、全取消
56             var $button2 =  $("input[value='全取消']");
57             $button2.click(function(){
58                 //alert("全取消");
59                 $("input:checkbox").removeAttr("checked");
60                 
61                 //全选中按钮生效
62                 $button1.removeAttr("disabled");
63                 //全取消失效
64                 $(this).attr("disabled","disabled");
65             });
66             
67             //3、全反选
68             var $button3 =  $("input:button").last();
69             $button3.click(function(){
70                 //alert("全反选");
71                 $(":checkbox:checked").attr("disabled","disabled");
72                 $(":checkbox:not(:checked)").attr("checked","checked");
73                 $(":checkbox:disabled").removeAttr("disabled").removeAttr("checked");
74             });
75     </script>
76   </body>
77 </html>

 

注意:在ie6下可以正常运行。但到了ie10,firefox高版本后,只能选一次,后面就不能选了。

 

posted @ 2013-05-10 19:55  hacket520  阅读(285)  评论(0编辑  收藏  举报