做后台的时候,用了网上下下来的一位网友制作的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;
- };