zoukankan      html  css  js  c++  java
  • js实现全选,反选,全不选

    思路: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">

    自立更生,艰苦奋斗!
  • 相关阅读:
    C语言中的排序算法--冒泡排序,选择排序,希尔排序
    常见算法:C语言求最小公倍数和最大公约数三种算法
    提高软件测试效率的方法探讨
    面试官询问的刁钻问题——以及如何巧妙地应付它们
    软件测试面试--如何测试网页的登录页面
    如何衡量测试效率,如何提高测试效率!
    利用交叉测试提升软件测试效率
    交叉测试的必要性和遇到的问题
    敏捷测试
    HttpWatch工具简介及使用技巧
  • 原文地址:https://www.cnblogs.com/javazxy/p/7072661.html
Copyright © 2011-2022 走看看