人人商城

cascade.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @name jQuery Cascdejs plugin
  3. * @author zdy
  4. * @version 1.0
  5. */
  6. //首先需要初始化
  7. var xmlDoc;
  8. var TopnodeList;
  9. var citys;
  10. var countyNodes;
  11. var nodeindex = 0;
  12. var childnodeindex = 0;
  13. //获取xml文件
  14. function cascdeInit(v1,v2,v3) {
  15. //打开xlmdocm文档
  16. xmlDoc = loadXmlFile('./resource/components/area/Area.xml');
  17. var dropElement1 = document.getElementById("sel-provance");
  18. var dropElement2 = document.getElementById("sel-city");
  19. var dropElement3 = document.getElementById("sel-area");
  20. RemoveDropDownList(dropElement1);
  21. RemoveDropDownList(dropElement2);
  22. RemoveDropDownList(dropElement3);
  23. if (window.ActiveXObject) {
  24. TopnodeList = xmlDoc.selectSingleNode("address").childNodes;
  25. }
  26. else {
  27. TopnodeList = xmlDoc.childNodes[0].getElementsByTagName("province");
  28. }
  29. if (TopnodeList.length > 0) {
  30. //省份列表
  31. var county;
  32. var province;
  33. var city;
  34. for (var i = 0; i < TopnodeList.length; i++) {
  35. //添加列表项目
  36. county = TopnodeList[i];
  37. var option = document.createElement("option");
  38. option.value = county.getAttribute("name");
  39. option.text = county.getAttribute("name");
  40. if (v1 == option.value) {
  41. option.selected = true;
  42. nodeindex = i;
  43. }
  44. dropElement1.add(option);
  45. }
  46. if (TopnodeList.length > 0) {
  47. //城市列表
  48. citys = TopnodeList[nodeindex].getElementsByTagName("city")
  49. for (var i = 0; i < citys.length; i++) {
  50. var id = dropElement1.options[nodeindex].value;
  51. //默认为第一个省份的城市
  52. province = TopnodeList[nodeindex].getElementsByTagName("city");
  53. var option = document.createElement("option");
  54. option.value = province[i] .getAttribute("name");
  55. option.text = province[i].getAttribute("name");
  56. if (v2 == option.value) {
  57. option.selected = true;
  58. childnodeindex = i;
  59. }
  60. dropElement2.add(option);
  61. }
  62. selectcounty(v3);
  63. }
  64. }
  65. }
  66. /*
  67. //依据省设置城市,县
  68. */
  69. function selectCity() {
  70. var dropElement1 = document.getElementById("sel-provance");
  71. var name = dropElement1.options[dropElement1.selectedIndex].value;
  72. countyNodes = TopnodeList[dropElement1.selectedIndex];
  73. var province = document.getElementById("sel-city");
  74. var city = document.getElementById("sel-area");
  75. RemoveDropDownList(province);
  76. RemoveDropDownList(city);
  77. var citynodes;
  78. var countycodes;
  79. if (window.ActiveXObject) {
  80. citynodes = xmlDoc.selectSingleNode('//address/province [@name="' + name + '"]').childNodes;
  81. } else {
  82. citynodes = countyNodes.getElementsByTagName("city")
  83. }
  84. if (window.ActiveXObject) {
  85. countycodes = citynodes[0].childNodes;
  86. } else {
  87. countycodes = citynodes[0].getElementsByTagName("county")
  88. }
  89. if (citynodes.length > 0) {
  90. //城市
  91. for (var i = 0; i < citynodes.length; i++) {
  92. var provinceNode = citynodes[i];
  93. var option = document.createElement("option");
  94. option.value = provinceNode.getAttribute("name");
  95. option.text = provinceNode.getAttribute("name");
  96. province.add(option);
  97. }
  98. if (countycodes.length > 0) {
  99. //填充选择省份的第一个城市的县列表
  100. for (var i = 0; i < countycodes.length; i++) {
  101. var dropElement2 = document.getElementById("sel-city");
  102. var dropElement3 = document.getElementById("sel-area");
  103. //取当天省份下第一个城市列表
  104. //alert(cityNode.childNodes.length);
  105. var option = document.createElement("option");
  106. option.value = countycodes[i].getAttribute("name");
  107. option.text = countycodes[i].getAttribute("name");
  108. dropElement3.add(option);
  109. }
  110. }
  111. }
  112. }
  113. /*
  114. //设置县,区
  115. */
  116. function selectcounty(v3) {
  117. var dropElement1 = document.getElementById("sel-provance");
  118. var dropElement2 = document.getElementById("sel-city");
  119. var name = dropElement2.options[dropElement2.selectedIndex].value;
  120. var city = document.getElementById("sel-area");
  121. var countys = TopnodeList[dropElement1.selectedIndex].getElementsByTagName("city")[dropElement2.selectedIndex].getElementsByTagName("county")
  122. RemoveDropDownList(city);
  123. for (var i = 0; i < countys.length; i++) {
  124. var countyNode = countys[i];
  125. var option = document.createElement("option");
  126. option.value = countyNode.getAttribute("name");
  127. option.text = countyNode.getAttribute("name");
  128. if(v3==option.value){
  129. option.selected=true;
  130. }
  131. city.add(option);
  132. }
  133. }
  134. function RemoveDropDownList(obj) {
  135. if (obj) {
  136. var len = obj.options.length;
  137. if (len > 0) {
  138. for (var i = len; i >= 0; i--) {
  139. obj.remove(i);
  140. }
  141. }
  142. }
  143. }
  144. /*
  145. //读取xml文件
  146. */
  147. function loadXmlFile(xmlFile) {
  148. var xmlDom = null;
  149. if (window.ActiveXObject) {
  150. xmlDom = new ActiveXObject("Microsoft.XMLDOM");
  151. xmlDom.async = false;
  152. xmlDom.load(xmlFile) || xmlDom.loadXML(xmlFile);//如果用的是XML字符串//如果用的是xml文件
  153. } else if (document.implementation && document.implementation.createDocument) {
  154. var xmlhttp = new window.XMLHttpRequest();
  155. xmlhttp.open("GET", xmlFile, false);
  156. xmlhttp.send(null);
  157. xmlDom = xmlhttp.responseXML;
  158. } else {
  159. xmlDom = null;
  160. }
  161. return xmlDom;
  162. }