area_form.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * @name jQuery Cascdejs plugin
  3. * @author zdy
  4. * @version 1.0
  5. */
  6. //首先需要初始化
  7. var xmlDoc;
  8. var TopnodeList;
  9. var CityList;
  10. var citys;
  11. var countyNodes;
  12. var nodeindex = 0;
  13. var childnodeindex = 0;
  14. var new_area = 0;
  15. var open_street = 0;
  16. //获取xml文件
  17. function cascdeInit(na,os, parent_id, v1,v2,v3,v4,v5) {
  18. id1= ".sel-province";
  19. id2= ".sel-city";
  20. id3= ".sel-area";
  21. id4= ".sel-street";
  22. new_area = na;
  23. open_street = os;
  24. if (v5) {
  25. id1 += v5;
  26. id2 += v5;
  27. id3 += v5;
  28. id4 += v5;
  29. }
  30. //打开xlmdocm文档
  31. if (new_area == 1) {
  32. var xmlfile = '/static/js/dist/area/AreaNew.xml';
  33. } else {
  34. var xmlfile = '/static/js/dist/area/Area.xml';
  35. }
  36. xmlDoc = loadXmlFile(xmlfile);
  37. var dropElement1 = $('#'+parent_id).find(id1)[0];
  38. var dropElement2 = $('#'+parent_id).find(id2)[0];
  39. var dropElement3 = $('#'+parent_id).find(id3)[0];
  40. var dropElement4 = $('#'+parent_id).find(id4)[0];
  41. RemoveDropDownList(dropElement1);
  42. RemoveDropDownList(dropElement2);
  43. RemoveDropDownList(dropElement3);
  44. RemoveDropDownList(dropElement4);
  45. if (window.ActiveXObject) {
  46. TopnodeList = xmlDoc.selectSingleNode("address").childNodes;
  47. }
  48. else {
  49. TopnodeList = xmlDoc.childNodes[0].getElementsByTagName("province");
  50. }
  51. if (TopnodeList.length > 0) {
  52. //省份列表
  53. var county;
  54. var province;
  55. var city;
  56. for (var i = 0; i < TopnodeList.length; i++) {
  57. //添加列表项目
  58. county = TopnodeList[i];
  59. var option = document.createElement("option");
  60. option.value = county.getAttribute("name");
  61. option.text = county.getAttribute("name");
  62. if (v1 == option.value) {
  63. option.selected = true;
  64. nodeindex = i;
  65. }
  66. dropElement1.add(option);
  67. }
  68. if (TopnodeList.length > 0) {
  69. //城市列表
  70. citys = TopnodeList[nodeindex].getElementsByTagName("city");
  71. for (var i = 0; i < citys.length; i++) {
  72. var id = dropElement1.options[nodeindex].value;
  73. //默认为第一个省份的城市
  74. province = TopnodeList[nodeindex].getElementsByTagName("city");
  75. var option = document.createElement("option");
  76. option.value = province[i] .getAttribute("name");
  77. option.text = province[i].getAttribute("name");
  78. if (v2 == option.value) {
  79. option.selected = true;
  80. childnodeindex = i;
  81. }
  82. dropElement2.add(option);
  83. }
  84. selectcounty(parent_id, v3,v4,v5);
  85. }
  86. }
  87. }
  88. /*
  89. //依据省设置城市,县
  90. */
  91. function selectCity(parent_id,v5) {
  92. id1= ".sel-province";
  93. id2= ".sel-city";
  94. id3= ".sel-area";
  95. id4= ".sel-street";
  96. if (v5) {
  97. id1 += v5;
  98. id2 += v5;
  99. id3 += v5;
  100. id4 += v5;
  101. }
  102. var dropElement1 = $('#'+parent_id).find(id1)[0];
  103. var name = dropElement1.options[dropElement1.selectedIndex].value;
  104. countyNodes = TopnodeList[dropElement1.selectedIndex];
  105. var province = $('#'+parent_id).find(id2)[0];
  106. var city = $('#'+parent_id).find(id3)[0];
  107. RemoveDropDownList(province);
  108. RemoveDropDownList(city);
  109. var citynodes;
  110. var countycodes;
  111. if (window.ActiveXObject) {
  112. citynodes = xmlDoc.selectSingleNode('//address/province [@name="' + name + '"]').childNodes;
  113. } else {
  114. citynodes = countyNodes.getElementsByTagName("city")
  115. }
  116. if (window.ActiveXObject) {
  117. countycodes = citynodes[0].childNodes;
  118. } else {
  119. countycodes = citynodes[0].getElementsByTagName("county")
  120. }
  121. if (citynodes.length > 0) {
  122. //城市
  123. for (var i = 0; i < citynodes.length; i++) {
  124. var provinceNode = citynodes[i];
  125. var option = document.createElement("option");
  126. option.value = provinceNode.getAttribute("name");
  127. option.text = provinceNode.getAttribute("name");
  128. province.add(option);
  129. }
  130. if (countycodes.length > 0) {
  131. //填充选择省份的第一个城市的县列表
  132. for (var i = 0; i < countycodes.length; i++) {
  133. var dropElement2 = $('#'+parent_id).find(id2);
  134. var dropElement3 = $('#'+parent_id).find(id3);
  135. //取当天省份下第一个城市列表
  136. //alert(cityNode.childNodes.length);
  137. var option = document.createElement("option");
  138. option.value = countycodes[i].getAttribute("name");
  139. option.text = countycodes[i].getAttribute("name");
  140. dropElement3.add(option);
  141. }
  142. }
  143. selectcounty(parent_id, 0,0,v5);
  144. }
  145. }
  146. /*
  147. //设置县,区
  148. */
  149. function selectcounty(parent_id, v3,v4,v5) {
  150. id1= ".sel-province";
  151. id2= ".sel-city";
  152. id3= ".sel-area";
  153. id4= ".sel-street";
  154. if (v5) {
  155. id1 += v5;
  156. id2 += v5;
  157. id3 += v5;
  158. id4 += v5;
  159. }
  160. var dropElement1 = $('#'+parent_id).find(id1)[0];
  161. var dropElement2 = $('#'+parent_id).find(id2)[0];
  162. var name = dropElement2.options[dropElement2.selectedIndex].value;
  163. var dropElement3 = $('#'+parent_id).find(id3)[0];
  164. var countys = TopnodeList[dropElement1.selectedIndex].getElementsByTagName("city")[dropElement2.selectedIndex].getElementsByTagName("county");
  165. if (new_area == 1 && open_street == 1) {
  166. var city_code = TopnodeList[dropElement1.selectedIndex].getElementsByTagName("city")[dropElement2.selectedIndex].getAttribute("code");
  167. if (city_code) {
  168. var left = city_code.substring(0,2);
  169. var xmlUrl = '/static/js/dist/area/list/'+left+'/'+city_code+'.xml';
  170. xmlCityDoc = loadXmlFile(xmlUrl);
  171. if (window.ActiveXObject) {
  172. CityList = xmlCityDoc.selectSingleNode("address").childNodes.childNodes;
  173. } else {
  174. CityList = xmlCityDoc.childNodes[0].getElementsByTagName("county");
  175. }
  176. }
  177. }
  178. RemoveDropDownList(dropElement3);
  179. if (countys.length > 0) {
  180. for (var i = 0; i < countys.length; i++) {
  181. var countyNode = countys[i];
  182. var option = document.createElement("option");
  183. option.value = countyNode.getAttribute("name");
  184. option.text = countyNode.getAttribute("name");
  185. if(v3==option.value){
  186. option.selected=true;
  187. }
  188. dropElement3.add(option);
  189. }
  190. if (new_area == 1 && open_street == 1) {
  191. selectstreet(parent_id,v4,v5);
  192. }
  193. }
  194. }
  195. function selectstreet(parent_id,v4,v5) {
  196. id1= ".sel-province";
  197. id2= ".sel-city";
  198. id3= ".sel-area";
  199. id4= ".sel-street";
  200. if (v5) {
  201. id1 += v5;
  202. id2 += v5;
  203. id3 += v5;
  204. id4 += v5;
  205. }
  206. var dropElement1 = $('#'+parent_id).find(id1)[0];
  207. var dropElement2 = $('#'+parent_id).find(id2)[0];
  208. var name = dropElement2.options[dropElement2.selectedIndex].value;
  209. var dropElement3 = $('#'+parent_id).find(id3)[0];
  210. var dropElement4 = $('#'+parent_id).find(id4)[0];
  211. var area = dropElement3.options[dropElement3.selectedIndex].value;
  212. var area_code = TopnodeList[dropElement1.selectedIndex].getElementsByTagName("city")[dropElement2.selectedIndex].getElementsByTagName("county")[dropElement3.selectedIndex].getAttribute("code");
  213. RemoveDropDownList(dropElement4);
  214. if(CityList && CityList.length>0 && area_code != null) {
  215. for (var i = 0; i < CityList.length; i++) {
  216. var county = CityList[i];
  217. var county_code = county.getAttribute("code");
  218. if(county_code == area_code){
  219. var streetlist = county.getElementsByTagName("street");
  220. for (var m = 0; m < streetlist.length; m++) {
  221. var street = streetlist[m];
  222. var option = document.createElement("option");
  223. option.value = street.getAttribute("name");
  224. option.text = street.getAttribute("name");
  225. if (v4 == option.value) {
  226. option.selected = true;
  227. nodeindex = m;
  228. }
  229. dropElement4.add(option);
  230. }
  231. }
  232. }
  233. }else{
  234. var option = document.createElement("option");
  235. option.value = '请选择街道/镇';
  236. option.text = '请选择街道/镇';
  237. dropElement4.add(option);
  238. }
  239. }
  240. function RemoveDropDownList(obj) {
  241. if (obj && obj.options) {
  242. var len = obj.options.length;
  243. if (len > 0) {
  244. for (var i = len; i >= 0; i--) {
  245. obj.remove(i);
  246. }
  247. }
  248. }
  249. }
  250. /*
  251. //读取xml文件
  252. */
  253. function loadXmlFile(xmlFile) {
  254. var xmlDom = null;
  255. if (window.ActiveXObject) {
  256. xmlDom = new ActiveXObject("Microsoft.XMLDOM");
  257. xmlDom.async = false;
  258. xmlDom.load(xmlFile) || xmlDom.loadXML(xmlFile);//如果用的是XML字符串//如果用的是xml文件
  259. } else if (document.implementation && document.implementation.createDocument) {
  260. var xmlhttp = new window.XMLHttpRequest();
  261. xmlhttp.open("GET", xmlFile, false);
  262. xmlhttp.send(null);
  263. xmlDom = xmlhttp.responseXML;
  264. } else {
  265. xmlDom = null;
  266. }
  267. return xmlDom;
  268. }