溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

bootstrap iCheck插件 全選和獲取value值的解決方法

發(fā)布時間:2020-07-19 23:00:20 來源:網(wǎng)絡 閱讀:3306 作者:zhaogaolong8 欄目:web開發(fā)

在使用jQuery iCheck 插件的時候遇到了一個問題,就是當我們使用普通的js全選功能無效了。

$("#checkall").click(
    function(){
        if(this.checked){
            $("input[name='checkname']").each(function(){this.checked=true;});
        }else{
            $("input[name='checkname']").each(function(){this.checked=false;});
        }
    }
);


這樣來寫對默認的checkbox框沒問題,但是當使用iCheck 插件后將無效。

那么該怎么解決呢?

最后是在stackoverflow 找到的解決方法:

地址是這里: http://stackoverflow.com/questions/17820080/function-select-all-and-icheck

 


//全選獲取數(shù)值
  var checkAll = $('input.all');
  var checkboxes = $('input.check');
  checkAll.on('ifChecked ifUnchecked', function(event) {
    if (event.type == 'ifChecked') {
      checkboxes.iCheck('check');
    } else {
      checkboxes.iCheck('uncheck');
    }
  });
  checkboxes.on('ifChanged', function(event){
    if(checkboxes.filter(':checked').length == checkboxes.length) {
      checkAll.prop('checked', 'checked');
    } else {
      checkAll.removeProp('checked');
    }
    checkAll.iCheck('update');
  });


在解決了全選問題后,又遇到了一個新的問題,獲取選中的checkbox的value的時候,使用:$(this).attr('checked');獲取不到值了~,蛋疼。

最后幾經(jīng)Google搜索,還是在stackoverflow 找到了啟發(fā),判斷checkbox的布爾值,使用 :$(this).is(':checked');

最后代碼的解決方法如下:

$("#manager_click").parent().siblings(".btn-primary").click(function(){
  // var url = $(this).attr('data-url');
  var str="";
  var ids="";
  $("#manager_table td.check-mail input").each(function(){
    if(true == $(this).is(':checked')){
      str+=$(this).parent().parent().siblings(".mail-ontact").text()+",";
    }
  });
  if(str.substr(str.length-1)== ','){
    ids = str.substr(0,str.length-1);
  }
  console.log(str);
  console.log(ids);
});










向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI