mlselection.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* 多级选择相关函数,如地区选择,分类选择
  2. * multi-level selection
  3. */
  4. /* 地区选择函数 */
  5. function regionInit(divId) {
  6. var area_id = 0;
  7. getArea(function(){
  8. if(typeof(ds_a[area_id]) == 'object' && ds_a[area_id].length > 0){//数组存在
  9. var area_select = $("#" + divId + " > select");//选择要初始化的对象
  10. areaInit(area_select,area_id);
  11. }
  12. $("#" + divId + " > select").change(regionChange); // select的onchange事件
  13. $("#" + divId + " > input:button[class='edit_region']").click(regionEdit); // 编辑按钮的onclick事件
  14. });
  15. }
  16. function areaInit(area_select,area_id){//初始化地区
  17. getArea(function(){
  18. if(typeof(area_select) == 'object' && ds_a[area_id].length > 0){
  19. var areas = new Array();
  20. areas = ds_a[area_id];
  21. $(area_select).append("<option>-请选择-</option>");
  22. for (i = 0; i <areas.length; i++){
  23. $(area_select).append("<option value='" + areas[i][0] + "'>" + areas[i][1] + "</option>");
  24. }
  25. }
  26. });
  27. }
  28. if(typeof(regionChange) != 'function'){//检测是否已经被定义过,防止重写
  29. function regionChange(){
  30. // 删除后面的select
  31. $(this).nextAll("select").remove();
  32. // 计算当前选中到id和拼起来的name
  33. var selects = $(this).siblings("select").andSelf();
  34. var id = '';
  35. var name='';
  36. var names = new Array();
  37. for (i = 0; i < selects.length; i++){
  38. sel = selects[i];
  39. if (sel.value > 0){
  40. id = sel.value;
  41. name = sel.options[sel.selectedIndex].text;
  42. names.push(name);
  43. }
  44. }
  45. $(".area_ids").val(id);
  46. $(".area_name").val(name);
  47. $(".area_names").val(names.join(" "));
  48. if (this.value > 0){//下级地区
  49. var area_id = this.value;
  50. if(typeof(ds_a[area_id]) == 'object' && ds_a[area_id].length > 0){//数组存在
  51. $("<select></select>").change(regionChange).insertAfter(this);
  52. areaInit($(this).next("select"),area_id);//初始化地区
  53. }
  54. }
  55. }
  56. }
  57. function getArea(callback){
  58. if(typeof(ds_a) == 'undefined'){//加载地区数据
  59. var script = document.createElement('script')
  60. script.src = '/static/plugins/area_datas.js'
  61. document.body.appendChild(script)
  62. script.onload = function () {
  63. callback()
  64. }
  65. } else {
  66. callback();
  67. }
  68. }
  69. function regionChange() {
  70. // 删除后面的select
  71. $(this).nextAll("select").remove();
  72. // 计算当前选中到id和拼起来的name
  73. var selects = $(this).siblings("select").andSelf();
  74. var id = 0;
  75. var name='';
  76. var names = new Array();
  77. for (i = 0; i < selects.length; i++) {
  78. sel = selects[i];
  79. if (sel.value > 0) {
  80. id = sel.value;
  81. name = sel.options[sel.selectedIndex].text;
  82. names.push(name);
  83. }
  84. }
  85. $(".area_ids").val(id);
  86. $(".area_name").val(name);
  87. $(".area_names").val(names.join(" "));
  88. // ajax请求下级地区
  89. if (this.value > 0) {
  90. var _self = this;
  91. var url = HOMESITEURL + '/Mlselection/index/type/region.html';
  92. $.getJSON(url, {'pid': this.value}, function (data) {
  93. if (data.code == 10000) {
  94. if (data.result.length > 0) {
  95. $("<select><option>" + '请选择默认' + "</option></select>").change(regionChange).insertAfter(_self);
  96. var data = data.result;
  97. for (i = 0; i < data.length; i++) {
  98. $(_self).next("select").append("<option value='" + data[i].area_id + "'>" + data[i].area_name + "</option>");
  99. }
  100. }
  101. }
  102. else {
  103. alert(data.message);
  104. }
  105. });
  106. }
  107. }
  108. function regionEdit() {
  109. $(this).siblings("select").show();
  110. $(this).siblings("span").andSelf().hide();
  111. }
  112. /* 商品分类选择函数 */
  113. function gcategoryInit(divId) {
  114. $("#" + divId + " > select").get(0).onchange = gcategoryChange; // select的onchange事件
  115. window.onerror = function () {
  116. return true;
  117. }; //屏蔽jquery报错
  118. $("#" + divId + " .edit_gcategory").click(gcategoryEdit); // 编辑按钮的onclick事件
  119. }
  120. function gcategoryChange() {
  121. // 删除后面的select
  122. $(this).nextAll("select").remove();
  123. // 计算当前选中到id和拼起来的name
  124. var selects = $(this).siblings("select").andSelf();
  125. var id = 0;
  126. var name='';
  127. var names = new Array();
  128. for (i = 0; i < selects.length; i++) {
  129. sel = selects[i];
  130. if (sel.value > 0) {
  131. id = sel.value;
  132. name = sel.options[sel.selectedIndex].text;
  133. names.push(name);
  134. }
  135. }
  136. $(".mls_id").val(id);
  137. $(".mls_name").val(name);
  138. $(".mls_names").val(names.join("\t"));
  139. // ajax请求下级分类
  140. if (this.value > 0) {
  141. var _self = this;
  142. var url = HOMESITEURL + '/Mlselection/index/type/goodsclass.html';
  143. $.getJSON(url, {'pid': this.value}, function (data) {
  144. if (data.code == 10000) {
  145. if (data.result.length > 0) {
  146. $("<select><option>" + "请选择默认" + "</option></select>").change(gcategoryChange).insertAfter(_self);
  147. var data = data.result;
  148. for (i = 0; i < data.length; i++) {
  149. $(_self).next("select").append("<option data-explain='" + data[i].commis_rate + "'value='" + data[i].gc_id + "'>" + data[i].gc_name + "</option>");
  150. }
  151. }
  152. }
  153. else {
  154. alert(data.message);
  155. }
  156. });
  157. }
  158. }
  159. function gcategoryEdit() {
  160. $(this).siblings("select").show();
  161. $(this).siblings("span").andSelf().remove();
  162. }
  163. //显示一级分类下拉框
  164. function show_gc_1(depth,gc_json){
  165. var html = '<select name="search_gc[]" id="search_gc_0" ds_type="search_gc" class="querySelect" onchange="change_gcselect(this)">';;
  166. html += ('<option value="0">请选择...</option>');
  167. if(gc_json){
  168. for(var i in gc_json){
  169. if(gc_json[i].depth == 1){
  170. html += ('<option value="'+gc_json[i].gc_id+'">'+gc_json[i].gc_name+'</option>');
  171. }
  172. }
  173. }
  174. html += '</select>';
  175. $("#searchgc_td").html(html);
  176. }
  177. //显示子分类下拉框
  178. function show_gc_2(chooseid,gc_json){
  179. if(gc_json && chooseid > 0){
  180. var childid = gc_json[chooseid].child;
  181. if(childid){
  182. var html = '<select name="search_gc[]" id="search_gc_'+gc_json[chooseid].depth+'" ds_type="search_gc" class="querySelect" onchange="change_gcselect(this)">';;
  183. html += ('<option value="0">请选择...</option>');
  184. var childid_arr = childid.split(",");
  185. if(childid_arr){
  186. for(var i in childid_arr){
  187. html += ('<option value="'+gc_json[childid_arr[i]].gc_id+'">'+gc_json[childid_arr[i]].gc_name+'</option>');
  188. }
  189. }
  190. html += '</select>';
  191. $("#searchgc_td").append(html);
  192. }
  193. }
  194. }
  195. var global_gc_json;
  196. function init_gcselect(chooseid_json, gc_json) {
  197. global_gc_json=gc_json
  198. show_gc_1(1, gc_json);
  199. if (chooseid_json) {
  200. for (var i in chooseid_json) {
  201. show_gc_2(chooseid_json[i], gc_json);
  202. $('#search_gc_' + i).val(chooseid_json[i]);
  203. $('#choose_gcid').val(chooseid_json[i]);
  204. }
  205. }
  206. }
  207. function change_gcselect(obj){
  208. $(obj).nextAll("[ds_type='search_gc']").remove();
  209. var chooseid = $(obj).val();
  210. if (chooseid > 0) {
  211. $("#choose_gcid").val(chooseid);
  212. show_gc_2(chooseid, global_gc_json);
  213. } else {
  214. chooseid = $(obj).prev().val();
  215. $("#choose_gcid").val(chooseid);
  216. }
  217. }