jquery 全选插件

XML/HTML Code
- <form>
- <input id="demo" type="checkbox"> Check All
- <input type="checkbox" value="1"> option 1
- <input type="checkbox" value="2"> option 2
- <input type="checkbox" value="3"> option 3
- </form>
JavaScript Code
- ;(function ($, window, document) {
- var pluginName = 'checkAll';
- var defaults = {
- container: document,
- childCheckboxes: 'input[type=checkbox]',
- showIndeterminate: false
- };
- function checkAll(element, options) {
- this.$el = $(element);
- this.options = $.extend({}, defaults, options) ;
- this.init();
- }
- checkAll.prototype.init = function() {
- this._checkChildren();
- var plugin = this;
- this.$el.on('change', function(e) {
- var $children = $(plugin.options.childCheckboxes, plugin.options.container).not(plugin.$el);
- $children.prop('checked', $(this).prop('checked'));
- });
- $(this.options.container).on('change', plugin.options.childCheckboxes, function(e) {
- plugin._checkChildren();
- });
- };
- // prevent multiple instantiations
- $.fn[pluginName] = function (options) {
- return this.each(function() {
- if (!$.data(this, 'plugin_' + pluginName)) {
- $.data(this, 'plugin_' + pluginName,
- new checkAll(this, options));
- }
- });
- }
- checkAll.prototype._checkChildren = function() {
- var totalCount = $(this.options.childCheckboxes, this.options.container).not(this.$el).length;
- var checkedCount = $(this.options.childCheckboxes,this.options.container)
- .filter(':checked').not(this.$el).length;
- var indeterminate = this.options.showIndeterminate &&
- checkedCount > 0 && checkedCount < totalCount;
- this.$el.prop('indeterminate', indeterminate);
- this.$el.prop('checked', checkedCount === totalCount);
- }
- })(jQuery, window, document);
原文地址:http://www.freejs.net/article_biaodan_358.html
最近更新
- jQuery轮播图插件slider-...
- CSS3鼠标悬停图片遮罩层变形动画特...
- 响应式全屏手风琴菜单,同时支持垂直方...
- 分组select选择器,支持多选和单...
- jQuery时间日期选择器代码日历插...
- Select 选择器 可以清空的单选...
我爱薅羊毛
点击最多
广告赞助
相关文章
- 美化input radio select等输入框,...
- select 下拉框多选,用select代替che...
- jQuery下拉多选插件 下拉框复选 包括全选
- radio单选框彩色,自定义边框,圆点颜色和大小
- 自定义checkbox和radio样式 圆形方形...
- select下拉菜单带图片