Witam

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:

  1. <script src="includes/javascript/jquery.js"></script>
  2. <script type="text/javascript">
  3. /*
  4. SelectBox jQuery Plugin
  5.  
  6. v1.0 (09.03.2013)
  7.  
  8. Created by gr56
  9.  
  10. Options
  11. structure: {
  12. 0: {
  13. name - string, name visible on list
  14. list - object, list of child elements
  15. }
  16. },
  17. size - int, value for select's size attribute
  18. nameFormat - string, e.g. "category[%id]"
  19. completed - function, the first parameter will contain results
  20.  
  21. */
  22. (function($) {
  23. $.fn.selectBox = function(options)
  24. {
  25. options = $.extend({
  26. structure: {},
  27. size: 1,
  28. nameFormat: "%id",
  29. active: [],
  30. completed: function(){}
  31. }, options);
  32.  
  33. var results = [];
  34.  
  35. for(var i=0; i < options['active'].length; i++)
  36. {
  37. var reference = options['structure'];
  38. for(var b=0; b < options['active'][i].length; b++)
  39. {
  40. if(b == 0)
  41. reference = reference[options['active'][i][b]];
  42. else
  43. reference = reference['list'][options['active'][i][b]];
  44. }
  45. reference['active'] = true;
  46. }
  47.  
  48. return this.each(function()
  49. {
  50. attachSelect($(this), options['structure'], 0);
  51. });
  52.  
  53. function attachSelect($container, data, index)
  54. {
  55. var $select = generateSelect(data, index);
  56. $container.append($select);
  57.  
  58. $select.on('change', function()
  59. {
  60. var val = $(this).val();
  61.  
  62. $(this).nextAll('select').remove();
  63. for(var i=index+1; i < results.length; i++)
  64. delete results[i];
  65.  
  66. if(val !== null)
  67. {
  68. results[index] = val;
  69. if(!$.isEmptyObject(data[val]['list']))
  70. {
  71. attachSelect($container, data[val]['list'], index+1);
  72. }
  73. else
  74. {
  75. options['completed'].call($container.get(0), results);
  76. }
  77. }
  78. }).trigger('change');
  79. }
  80.  
  81. function generateSelect(list, index)
  82. {
  83. var html = "";
  84. html += "<option value=''>-- wybierz --</option>";
  85. for(i in list)
  86. {
  87. html += "<option value='" + i + "' " + (list[i]['active'] ? "selected='selected'" : "") + ">" + list[i]['name'] + "</option>";
  88. delete list[i]['active'];
  89. }
  90.  
  91. return $('<select>')
  92. .attr('name', options['nameFormat'].replace("%id", index))
  93. .attr('size', options['size'])
  94. .html(html);
  95. }
  96. }
  97. })(jQuery);
  98. <script type="text/javascript">
  99. var structure = {
  100. 3: {
  101. name: "Małopolskie",
  102. list: {
  103. 3: { name: "Bało"},
  104. 4: { name: "BOBO"}
  105. }
  106. },
  107. };
  108. $(function()
  109. {
  110. var active=[]
  111. $('#lokalizacja').selectBox({
  112. structure: structure,
  113. nameFormat: "region[%id]",
  114. active: active
  115. });
  116.  
  117. $('#lokalizacja2').selectBox({
  118. structure: structure,
  119. nameFormat: "region[%id]",
  120. active: active
  121. });
  122. });

wywołuje DIVem z podanym ID:
  1. <div id="lokalizacja"></div>
  2. <div id="lokalizacja2"></div>