diff --git a/jquery.chained.js b/jquery.chained.js index 2364e96..dfc3447 100644 --- a/jquery.chained.js +++ b/jquery.chained.js @@ -47,22 +47,15 @@ $(child).html(backup.html()); /* If multiple parents build classname like foo\bar. */ - var selected = ""; + var selected = []; $(parent_selector).each(function() { - var selectedClass = $("option:selected", this).val(); - if (selectedClass) { - if (selected.length > 0) { - if (window.Zepto) { - /* Zepto class regexp dies with classes like foo\bar. */ - selected += "\\\\"; - } else { - selected += "\\"; - } - } - selected += selectedClass; - } + $(":selected", this).each(function() { + selected.push($(this).val()); + }); }); + + /* Also check for first parent without subclassing. */ /* TODO: This should be dynamic and check for each parent */ /* without subclassing. */ @@ -75,11 +68,16 @@ var selected_first = $("option:selected", first).val(); $("option", child).each(function() { - /* Remove unneeded items but save the default value. */ - if ($(this).hasClass(selected) && $(this).val() === currently_selected_value) { - $(this).prop("selected", true); - trigger_change = false; - } else if (!$(this).hasClass(selected) && !$(this).hasClass(selected_first) && $(this).val() !== "") { + var hasSelectedClass = false; + var classList = ($(this).attr('class') == undefined || "" ) ? [] : $(this).attr('class').split(/\s+/); + $.each( classList, function(index, item){ + if (jQuery.inArray(item, selected) >= 0) { + hasSelectedClass = true; + return; + } + }); + if (!hasSelectedClass && + !$(this).hasClass(selected_first) && $(this).val() !== "") { $(this).remove(); } }); diff --git a/jquery.chained.min.js b/jquery.chained.min.js index a70ce03..4710558 100644 --- a/jquery.chained.min.js +++ b/jquery.chained.min.js @@ -1,2 +1,3 @@ -/*! Chained 1.0.0 - MIT license - Copyright 2010-2017 Mika Tuupola */ -!function(a,b,c,d){"use strict";a.fn.chained=function(c){return this.each(function(){function d(){var d=!0,g=a("option:selected",e).val();a(e).html(f.html());var h="";a(c).each(function(){var c=a("option:selected",this).val();c&&(h.length>0&&(h+=b.Zepto?"\\\\":"\\"),h+=c)});var i;i=a.isArray(c)?a(c[0]).first():a(c).first();var j=a("option:selected",i).val();a("option",e).each(function(){a(this).hasClass(h)&&a(this).val()===g?(a(this).prop("selected",!0),d=!1):a(this).hasClass(h)||a(this).hasClass(j)||""===a(this).val()||a(this).remove()}),1===a("option",e).length&&""===a(e).val()?a(e).prop("disabled",!0):a(e).prop("disabled",!1),d&&a(e).trigger("change")}var e=this,f=a(e).clone();a(c).each(function(){a(this).bind("change",function(){d()}),a("option:selected",this).length||a("option",this).first().attr("selected","selected"),d()})})},a.fn.chainedTo=a.fn.chained,a.fn.chained.defaults={}}(window.jQuery||window.Zepto,window,document); \ No newline at end of file +/*! Chained 1.0.1 - MIT license - Copyright 2010-2017 Mika Tuupola */ + +!function(a,b,c,d){"use strict";a.fn.chained=function(b){return this.each(function(){function c(){a("option:selected",e).val();a(e).html(f.html());var c=[];a(b).each(function(){a(":selected",this).each(function(){c.push(a(this).val())})});var g;g=a.isArray(b)?a(b[0]).first():a(b).first();var h=a("option:selected",g).val();a("option",e).each(function(){var b=!1,e=a(this).attr("class")==d?[]:a(this).attr("class").split(/\s+/);a.each(e,function(a,d){if(jQuery.inArray(d,c)>=0)return void(b=!0)}),b||a(this).hasClass(h)||""===a(this).val()||a(this).remove()}),1===a("option",e).length&&""===a(e).val()?a(e).prop("disabled",!0):a(e).prop("disabled",!1),a(e).trigger("change")}var e=this,f=a(e).clone();a(b).each(function(){a(this).bind("change",function(){c()}),a("option:selected",this).length||a("option",this).first().attr("selected","selected"),c()})})},a.fn.chainedTo=a.fn.chained,a.fn.chained.defaults={}}(window.jQuery||window.Zepto,window,document); \ No newline at end of file diff --git a/jquery.chained.remote.js b/jquery.chained.remote.js index 46d2ff2..ed922e0 100644 --- a/jquery.chained.remote.js +++ b/jquery.chained.remote.js @@ -17,7 +17,6 @@ "use strict"; $.fn.remoteChained = function(options) { - var settings = $.extend({}, $.fn.remoteChained.defaults, options); /* Loading text always clears the select. */ @@ -38,7 +37,16 @@ var data = {}; $(settings.parents).each(function() { var id = $(this).attr(settings.attribute); - var value = ($(this).is("select") ? $(":selected", this) : $(this)).val(); + var value = [] + + if($(this).is("select")) { + $(":selected", this).each(function() { + value.push($(this).val()); + }); + } else { + value.push($(this).val()); + } + data[id] = value; /* Optionally also depend on values from these inputs. */ @@ -164,4 +172,4 @@ clear : false }; -})(window.jQuery || window.Zepto, window, document); \ No newline at end of file +})(window.jQuery || window.Zepto, window, document); diff --git a/jquery.chained.remote.min.js b/jquery.chained.remote.min.js index e89ce59..e8c2877 100644 --- a/jquery.chained.remote.min.js +++ b/jquery.chained.remote.min.js @@ -1,2 +1,3 @@ -/*! Chained 1.0.0 - MIT license - Copyright 2010-2017 Mika Tuupola */ -!function(a,b,c,d){"use strict";a.fn.remoteChained=function(b){var c=a.extend({},a.fn.remoteChained.defaults,b);return c.loading&&(c.clear=!0),this.each(function(){function b(b){var c=a(":selected",d).val();a("option",d).remove();var e=[];if(a.isArray(b))e=a.isArray(b[0])?b:a.map(b,function(b){return a.map(b,function(a,b){return[[b,a]]})});else for(var f in b)b.hasOwnProperty(f)&&e.push([f,b[f]]);for(var g=0;g!==e.length;g++){var h=e[g][0],i=e[g][1];if("selected"!==h){var j=a("