js中,checkBox中復選框實現批處理操作


(1)首先,在復選框中選擇要操作的對象,效果圖如下所示:

 

 

 

 

 

 

 

 

 

(2)html中,代碼如下:

 <table class="table table-striped table-bordered" id="dataTables" style="white-space: nowrap;width: 100%;">
      <thead>
          <tr style="background: #d9d6c3;">
              <th><input type="checkbox" id="checkAll" /></th>
              <th>文件名</th>
              <th style="width: 95px;">文件大小</th>
              <th style="width: 165px;">文件上傳時間</th>
          </tr>
      </thead>
      <tbody>
          <tr>
             <td><input type="checkbox" name="ck" id="ckc"/>
          <
input type="hidden" value="'+docId+'" id="documentId"/>
        </
td> <td>Torasemide ...</td> <td>180 kb</td> <td>2016-11-28</td> </tr> ......... </tbody> </table>

(3)js中,實現復選框的全選操作

$("#checkAll").bind("click", function () {
        if($(this).attr("checked") == 'checked'){
            $("[name = 'ck']:checkbox").attr("checked", true);
        } else {
            $("[name = 'ck']:checkbox").attr("checked", false);
        }
    });

(4)全選后的狀態,效果圖如下:

(5)對於全選狀態,對選擇的復選框進行操作,js中進行的操作如下:

說明:其中substring對字符串進行截取操作,lastIndexOf(',')是對字符串最后一個','位置

function openWay(obj){
    var trs = $("#dataTables tr"); var param1="";
    var param2="";
    //alert("=========");
    for(var i=0;i<trs.length;i++){                //對每一個選中的復選框進行操作(更為簡單的實現方式:見下面)
        var obs = $(trs[i]).find("#ckc").attr("checked"); if(obs == 'checked'){
          var documentId = $(trs[i]).find("#documentId").val();  //獲取復選框中的值(更為簡單的實現方式:見下面)   if(documentId!=''){ 
                param1+=documentId+"↔↓×↔";
            }else{
                param2+=documentId+"↔↓×↔";
            }
        }
    }
    
    if(param1.length<=0 && param2.length<=0){
        alert({msg:"請先選擇文件!"});
        return null;
    }
    if(param1.length>0){ param1=param1.substring(0,param1.lastIndexOf("↔↓×↔")); } if(param2.length>0){
        param2=param2.substring(0,param2.lastIndexOf("↔↓×↔"));
    }
    //此處可以操作其他內容,也可以進行數據庫操作    
}

 其他方式:實現多選框的選擇

$("[name='checkbox']").each(function(){//反選
  if($(this).attr("checked") ){
    $(this).attr("checked",false);
  }
});

獲取復選框中選中值:

var c = "";
$('input[name="op"]:checked').each(function() {
  c += $(this).val() + ",";         //this指當前點擊的對象
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM