做背景時候,用了網上下下來的一位網友製作的checkbox全選外掛程式,試用了一下,甚為難用,很不舒服,於是就他的模式重寫了一下,發出來共用!有四個函數,分別是全選、反選、全不選、取值!
- /**
- * checkbox 全選操作
- *
- * @author shaoyun(若水老人) <shaoyun at yeah.net>
- * @copyright Copyright (c) 2008 (http://www.devjs.com)
-
- * @example $('input[@type=checkbox][@name=checkAll]').checkbox();
- * 反選 : .toggle()
- * 全選 : .checked()
- * 全不選 : .unchecked()
- * 擷取字串值 : .val()
- */
-
- $.fn.checkbox = function(){
- // 反選
- this.toggle = function(ele){
- $(this).each(function(){
- if(this.checked){
- $(this).attr('checked',false);
- }else{
- $(this).attr('checked',true);
- }
- });
- };
- // 全選
- this.checked = function(){
- $(this).attr('checked', true);
- };
- // 全不選
- this.unchecked = function(ele){
- $(this).attr('checked', false);
- };
- // 擷取已選中值, 並以字串返回資料
- this.val = function(){
- var string = '';
- $(this).each(function(){
- if (this.checked && $(this).val()) {
- if (string) {
- string += ',';
- }
- string += $(this).val();
- };
- });
- return string;
- };
- return this;
- };