zoukankan      html  css  js  c++  java
  • 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");
               }
          }
     
    }
  • 相关阅读:
    ThinkPHP5专题
    php截取中文字符串
    跨域/非跨域接口专题
    JS检查输入项是否为手机号码或者固话号码的正则表达式
    TinkPHP去重统计查询
    模型类设计模式
    经典排序方法 python
    leetcode122 买卖股票的最佳时机 python
    找到链表的倒数第k个节点 python
    链表的实现、输出和反向 python
  • 原文地址:https://www.cnblogs.com/qifan/p/3793963.html
Copyright © 2011-2022 走看看