码迷,mamicode.com
首页 > Web开发 > 详细

js实现全选,反选,全不选

时间:2017-06-24 11:16:00      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:elements   循环   check   ges   判断   box   数组   通过   cti   

思路:1、获取元素。2、用for循环历遍数组,把checkbox的checked设置为true即实现全选,把checkbox的checked设置为false即实现不选。3、通过if判断,如果checked为true选中状态的,就把checked设为false不选状态,如果checked为false不选状态的,就把checked设为true选中状态。

js代码

 <script>
  window.onload=function(){
     var CheckAll=document.getElementById(‘All‘);
     var UnCheck=document.getElementById(‘uncheck‘);
    var OtherCheck=document.getElementById(‘othercheck‘);
     var div=document.getElementById(‘div‘);
     var    CheckBox=div.getElementsByTagName(‘input‘);
     CheckAll.onclick=function(){
             for(i=0;i<CheckBox.length;i++){
                    CheckBox[i].checked=true;
                 };
         };
     UnCheck.onclick=function(){
             for(i=0;i<CheckBox.length;i++){
                     CheckBox[i].checked=false;
                 };
         };
     othercheck.onclick=function(){
             for(i=0;i<CheckBox.length;i++){
                     if(CheckBox[i].checked==true){
                             CheckBox[i].checked=false;
                         }
                     else{
                         CheckBox[i].checked=true
                         }
                     
                 };
         };
 };
 </script>

html代码

全选:<input type="button" id="All" value="全选" /><br />
不选<input type="button" id="uncheck" value="不选" /><br />
 反选<input type="button" id="othercheck" value="反选" /><br />
 <div id="div">
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
     <input type="checkbox" /><br />
 </div>

运行效果

技术分享

这个button真是太丑了,个人喜欢用<input type="radio">

js实现全选,反选,全不选

标签:elements   循环   check   ges   判断   box   数组   通过   cti   

原文地址:http://www.cnblogs.com/javazxy/p/7072661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!