Mam problem z wprowadzeniem dwóch 'selectboxów' na jednej stronie - za każdym razem pojawia się tylko jeden (pierwszy) - zmieniałem już nazwy etc - bez rezultatów. Poniżej kod:
<script type="text/javascript"> /* SelectBox jQuery Plugin v1.0 (09.03.2013) Created by gr56 Options structure: { 0: { name - string, name visible on list list - object, list of child elements } }, size - int, value for select's size attribute nameFormat - string, e.g. "category[%id]" completed - function, the first parameter will contain results */ (function($) { $.fn.selectBox = function(options) { options = $.extend({ structure: {}, size: 1, nameFormat: "%id", active: [], completed: function(){} }, options); var results = []; for(var i=0; i < options['active'].length; i++) { var reference = options['structure']; for(var b=0; b < options['active'][i].length; b++) { if(b == 0) reference = reference[options['active'][i][b]]; else reference = reference['list'][options['active'][i][b]]; } reference['active'] = true; } return this.each(function() { attachSelect($(this), options['structure'], 0); }); function attachSelect($container, data, index) { var $select = generateSelect(data, index); $container.append($select); $select.on('change', function() { var val = $(this).val(); $(this).nextAll('select').remove(); for(var i=index+1; i < results.length; i++) delete results[i]; if(val !== null) { results[index] = val; if(!$.isEmptyObject(data[val]['list'])) { attachSelect($container, data[val]['list'], index+1); } else { options['completed'].call($container.get(0), results); } } }).trigger('change'); } function generateSelect(list, index) { var html = ""; html += "<option value=''>-- wybierz --</option>"; for(i in list) { delete list[i]['active']; } return $('<select>') .attr('name', options['nameFormat'].replace("%id", index)) .attr('size', options['size']) .html(html); } } })(jQuery); </script> <script type="text/javascript"> var structure = { 3: { name: "Małopolskie", list: { 3: { name: "Bało"}, 4: { name: "BOBO"} } }, }; </script> <script> $(function() { var active=[] $('#lokalizacja').selectBox({ structure: structure, nameFormat: "region[%id]", active: active }); $('#lokalizacja2').selectBox({ structure: structure, nameFormat: "region[%id]", active: active }); }); </script>
wywołuje DIVem z podanym ID: