多选全选jquery实现

来源:互联网 发布:htc windows mobile 编辑:程序博客网 时间:2024/06/06 10:48
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>多选反选</title>  
  6. <script type="text/javascript" src="../js/jquery-1.4.2.js"></script>  
  7. <script type="text/javascript">  
  8.     window.onload=function(){  
  9.         $("#yesorno").click(function(){  
  10.             $("input[name=sport]").attr("checked",$(this).attr("checked"));  
  11.         });  
  12.       
  13.       
  14.     $("#allselect").click(function(){  
  15.         $("input[name=sport]").attr("checked",true);  
  16.     });  
  17.     $("#nothingselect").click(function(){  
  18.         $("input[name=sport]").attr("checked",null);  
  19.     });  
  20.       
  21.     $("#reverseselect").click(function(){  
  22.         $("input[name=sport]").each(function(){  
  23.             if($(this).attr("checked")){  //if(this.checked){  
  24.                 $(this).attr("checked",null);  
  25.             }else{  
  26.                 $(this).attr("checked",true);  
  27.             }  
  28.         });  
  29.     });  
  30.     };  
  31. </script>  
  32. </head>  
  33. <body>  
  34.     <input type="checkbox" id="yesorno"/>全选/全不选 <br/>  
  35.       
  36.     <input type="checkbox" name="sport" id="football" />足球  
  37.     <input type="checkbox" name="sport" id="basketball"/>蓝球  
  38.     <input type="checkbox" name="sport" id="volleyball"/>排球  
  39.     <input type="checkbox" name="sport" id="tennis"/>网球  
  40.     <br/>  
  41.     <input type="button" id="allselect" value="全选"/>  
  42.     <input type="button" id="nothingselect" value="全不选"/>  
  43.     <input type="button" id="reverseselect" value="反选"/>  
  44.       
  45. </body>  
  46. </html>  
0 0
原创粉丝点击