• jQuery --checkbox全选和取消全选简洁高效的解决办法


      最近在公司做了一个小项目,其中有一个全选和取消全选的这么一个模块,搞了半天找不到一种最佳的解决方案!后来通过各种努力找到了一种简洁高效的解决办法,这里想和大家分享一下。有问题的话,还望各路大神指导一二。

    html代码如下:

    <fieldset data-role="controlgroup">  
      <label><input type="checkbox" name="boxes" id="select_all" onclick="selectAll();" >全选</label>   <label><input type="checkbox" name="box" onclick="select();" >子选项1</label>   <label><input type="checkbox" name="box" onclick="select();" >子选项2</label>
    </fieldset> 

    jquery代码如下:

    function selectAll(){
         if ($("#select_all").prop("checked")) {  
             $("input[name='box']").prop("checked", true).checkboxradio("refresh");  
         } else {  
             $("input[name='box']").prop("checked", false).checkboxradio("refresh"); 
         }  
    }
    /*如果子项全被选中或者某一个子项被取消,全选项相应的勾选或者全选相应取消勾选*/
    function select(){
          if ($("#select_all").prop("checked")){
              $("input[name='box']").each(function(){
                       if(this.checked == false)
                       {
                               $("input[name='boxes']").prop("checked", false).checkboxradio("refresh");
                       }
               });
          }
          else{
              var n = 0;
              $("input[name='box']").each(function(){
                       if(this.checked == false){
                           n++;
                       }
               });    
               if(!n){
                   $("input[name='boxes']").prop("checked", true).checkboxradio("refresh");
               }
          }
     
    }
  • 相关阅读:
    Spring学习记录(八)---Bean的生命周期
    Spring学习记录(七)---表达式语言-SpEL
    Spring学习记录(六)---使用外部属性文件
    Spring学习记录(五)---bean的作用域scope
    Spring学习记录(四)---bean之间的关系:继承、依赖
    Spring学习记录(三)---bean自动装配autowire
    Spring学习记录(二)---容器和bean属性配置
    2017.9
    Flask
    ELK
  • 原文地址:https://www.cnblogs.com/qifan/p/3793963.html
Copyright © 2020-2023  润新知