jquery.lSelect.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /***
  2. * lSelect(Link Select) 无限级联动下拉菜单插件
  3. *
  4. * JSON数据格式示例:[{"title": "北京", "value": "beijing"},{"title": "湖南", "value": "hunan"},{"title": "湖北", "value": "hubei"}]
  5. *
  6. **/
  7. (function($){
  8. $.extend($.fn, {
  9. lSelect: function(options){
  10. // 默认参数
  11. var settings = {
  12. url: "",// 数据获取url
  13. parameter: "path",// 数据获取参数名称
  14. title: "title",// 定义JSON数据格式:选择名称
  15. value: "value",// 定义JSON数据格式:选择值
  16. emptyOption: "请选择",// 选择提示,null表示无提示
  17. cssClass: "lSelect",// 下拉框css名称
  18. cssStyle: {"margin-right": "10px"},// 下拉框左右css样式
  19. isFadeIn: true// 是否渐显
  20. };
  21. $.extend(settings, options);
  22. return this.each(function(){
  23. var $this = $(this);
  24. $this.hide();
  25. var selectGroupClass = "lSelect" + Math.round(Math.random() * 1000000);
  26. var items = {};
  27. var selectName = $this.attr("name");
  28. var defaultSelectedValue = $this.val();
  29. if (defaultSelectedValue == "") {
  30. addSelect($this);
  31. } else {
  32. var $select = $this;
  33. var defaultSelectedValueArray = defaultSelectedValue.split(",");
  34. for (var i = 0; i < defaultSelectedValueArray.length; i++) {
  35. var $nextSelect = addSelect($select, defaultSelectedValueArray[i]);
  36. if($nextSelect) {
  37. $select = $nextSelect;
  38. }
  39. }
  40. }
  41. // 绑定Select元素
  42. function bindSelect(element) {
  43. element.bind("change", function(){
  44. addSelect(element);
  45. $this.val(element.val());
  46. });
  47. }
  48. // 获取Json数据
  49. function getJson(key) {
  50. if(typeof(items[key]) == "undefined") {
  51. var url = settings.url;
  52. if (key != "lSelectRoot") {
  53. var parameter = settings.parameter;
  54. if (parameter != null) {
  55. if(url.indexOf("?") > 0) {
  56. url = url + "&" + parameter + "=" + key;
  57. } else {
  58. url = url + "?" + parameter + "=" + key;
  59. }
  60. }
  61. }
  62. $.ajaxSetup({async: false});
  63. $.getJSON(url, function(json) {
  64. items[key] = json;
  65. });
  66. }
  67. return items[key];
  68. }
  69. // 填充option
  70. function fill(element, key, selected) {
  71. var json = getJson(key);
  72. if (!json) {
  73. return false;
  74. }
  75. var length = 0;
  76. for (j in json){
  77. length ++;
  78. }
  79. if (length == 0) {
  80. return false;
  81. } else {
  82. element.empty();
  83. if(settings.emptyOption != null) {
  84. element.append('<option value="">' + settings.emptyOption + '</option>');
  85. }
  86. $.each(json, function(id, object) {
  87. var optionValue = "";
  88. if (object.value.indexOf(",") >= 0) {
  89. var optionValueArray = object.value.split(",");
  90. optionValue = optionValueArray[optionValueArray.length - 1];
  91. } else {
  92. optionValue = object.value;
  93. }
  94. var option;
  95. if(selected && optionValue == selected) {
  96. option = $('<option value="' + object.value + '" selected>' + object.title + '</option>');
  97. } else {
  98. option = $('<option value="' + object.value + '">' + object.title + '</option>');
  99. }
  100. element.append(option);
  101. });
  102. return true;
  103. }
  104. }
  105. // 增加select
  106. function addSelect(element, selected) {
  107. var $nextSelect;
  108. var isFill;
  109. if(element.is("select")) {
  110. element.nextAll("." + selectGroupClass).remove();
  111. if(element.val() == "") {
  112. return;
  113. }
  114. element.after('<select class="' + settings.cssClass + ' ' + selectGroupClass + '" style="display: none;"></select>');
  115. $nextSelect = element.next("." + selectGroupClass);
  116. isFill = fill($nextSelect, element.val(), selected);
  117. } else {
  118. element.after('<select class="' + settings.cssClass + ' ' + selectGroupClass + '" style="display: none;"></select>');
  119. $nextSelect = element.next("." + selectGroupClass);
  120. isFill = fill($nextSelect, "lSelectRoot", selected);
  121. }
  122. if (isFill) {
  123. element.css(settings.cssStyle);
  124. if(settings.isFadeIn) {
  125. $nextSelect.fadeIn();
  126. bindSelect($nextSelect);
  127. } else {
  128. $nextSelect.show();
  129. }
  130. return $nextSelect;
  131. } else {
  132. $nextSelect.remove();
  133. }
  134. }
  135. });
  136. }
  137. });
  138. })(jQuery);