您好,登錄后才能下訂單哦!
在使用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); });
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。