现在的位置: 首页 > 综合 > 正文

jquery插件-checkbox全选插件

2012年09月19日 ⁄ 综合 ⁄ 共 1058字 ⁄ 字号 评论关闭

做后台的时候,用了网上下下来的一位网友制作的checkbox全选插件,试用了一下,甚为难用,很不舒服,于是就他的模式重写了一下,发出来共享!有四个函数,分别是全选、反选、全不选、取值!

  1. /**  
  2.  * checkbox 全选操作  
  3.  *  
  4.  * @author     shaoyun(若水老人) <shaoyun at yeah.net>  
  5.  * @copyright  Copyright (c) 2008 (http://www.devjs.com)  
  6.    
  7.  * @example $('input[@type=checkbox][@name=checkAll]').checkbox();  
  8.  * 反选 : .toggle()  
  9.  * 全选 : .checked()  
  10.  * 全不选 : .unchecked()  
  11.  * 获取字符串值 : .val()  
  12.  */ 
  13.  
  14. $.fn.checkbox = function(){  
  15.   // 反选  
  16.   this.toggle = function(ele){  
  17.     $(this).each(function(){  
  18.       if(this.checked){  
  19.         $(this).attr('checked',false);  
  20.       }else{  
  21.         $(this).attr('checked',true);  
  22.       }  
  23.     });  
  24.   };  
  25.   // 全选  
  26.   this.checked = function(){  
  27.     $(this).attr('checked'true);  
  28.   };  
  29.   // 全不选  
  30.   this.unchecked = function(ele){  
  31.     $(this).attr('checked'false);  
  32.   };  
  33.   // 获取已选中值, 并以字符串返回数据  
  34.   this.val = function(){  
  35.     var string = '';  
  36.     $(this).each(function(){  
  37.       if (this.checked && $(this).val()) {  
  38.         if (string) {  
  39.           string += ',';  
  40.         }  
  41.         string += $(this).val();  
  42.       };  
  43.     });  
  44.     return string;  
  45.   };  
  46.   return this;  
  47. }; 

抱歉!评论已关闭.