项目中经常用到,所用弄成插件使用
/*!
* copyright c by Ethan 2012-02-08
* jquery.checkAll.js 复选框全选功能
* v1.0 2012-02-08 create
* usage: $('table').checkAll();
*/
(function($) {
$.fn.checkAll = function(options) {
var defaults = {
checkAll:'thead :checkbox',
checkboxes:'tbody :checkbox'
};
var params = $.extend({}, defaults, options);
$(this).each(function(){
var $table = $(this),
$checkAll = $(params.checkAll, this),
$checkboxes = $(params.checkboxes, this);
$checkAll.click(function(){
if( $(this).attr('checked') === 'checked' ){
$checkboxes.attr('checked', true);
}else{
$checkboxes.attr('checked', false);
}
});
$checkboxes.click(function(){
if( $(this).attr('checked') !== 'checked' ){
$checkAll.attr('checked', false);
}else if( $checkboxes.filter(':checked').size() === $checkboxes.size() ){
$checkAll.attr('checked', true);
}
});
});
};
})(jQuery);