From a719c4b543a6032cb67b8e58046bcc9c908d7c96 Mon Sep 17 00:00:00 2001 From: Jacky Date: Wed, 30 Aug 2017 15:25:44 +0800 Subject: [PATCH 1/2] Update jquery-check-all.js add a function that can get checked items --- jquery-check-all.js | 108 ++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 50 deletions(-) diff --git a/jquery-check-all.js b/jquery-check-all.js index a4fdf8e..0478301 100644 --- a/jquery-check-all.js +++ b/jquery-check-all.js @@ -1,53 +1,61 @@ ;(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, this.$el.data(), 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 && totalCount !== 0); - } + var pluginName = 'checkAll'; + + var defaults = { + container: document, + childCheckboxes: 'input[type=checkbox]', + showIndeterminate: false + }; + + function CheckAll(element, options) { + this.$el = $(element); + this.options = $.extend({}, defaults, this.$el.data(), 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 && totalCount !== 0); + }; + + CheckAll.prototype.values = function () { + var ids = ''; + $(this.options.childCheckboxes, this.options.container).filter(':checked').not(this.$el).each(function (i, v) { + ids += $(v).val() + ','; + }); + return ids.slice(0, -1); + }; })(jQuery, window, document); From fcd1b92d7c19f5651e90a5c298582ed36d31f684 Mon Sep 17 00:00:00 2001 From: Jacky Date: Wed, 30 Aug 2017 15:27:42 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d5a0dc1..48ceaff 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ Call the `checkAll()` function on your "check all" checkbox to initialize the pl ```