jquery外掛程式-checkbox全選外掛程式

來源:互聯網
上載者:User

做背景時候,用了網上下下來的一位網友製作的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. }; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.