area.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 
  2. var areadata = function(o)
  3. {
  4. var _cmbProvince=o._cmbProvince,//省
  5. _cmbCity=o._cmbCity,//市
  6. _cmbArea=o._cmbArea,//县
  7. defaultProvince=o.defaultProvince,//默认省
  8. defaultCity=o.defaultCity,//默认市
  9. defaultArea=o.defaultArea,//默认市
  10. cmbProvince = document.getElementById(_cmbProvince),
  11. cmbCity = document.getElementById(_cmbCity),
  12. cmbArea = document.getElementById(_cmbArea),
  13. provinceList = [],
  14. xmlHttp = null;
  15. function cmbSelect(cmb, str)
  16. {
  17. for(var i=0; i<cmb.options.length; i++)
  18. {
  19. if(cmb.options[i].value == str)
  20. {
  21. cmb.selectedIndex = i;
  22. return;
  23. }
  24. }
  25. }
  26. function cmbAddOption(cmb, str, obj)
  27. {
  28. var option = document.createElement("OPTION");
  29. cmb.options.add(option);
  30. option.innerHTML = str;
  31. option.value = obj.id;
  32. option.obj = obj;
  33. }
  34. function changeCity()
  35. {
  36. if(cmbArea==undefined){return false;}
  37. cmbArea.options.length = 0;
  38. if(cmbCity.selectedIndex == -1)return;
  39. var item = cmbCity.options[cmbCity.selectedIndex].obj;
  40. for(var i=0; i<provinceList.length; i++)
  41. {
  42. if(provinceList[i].pid==item.id){
  43. if(provinceList[i].name=="市辖区"||provinceList[i].name=="市辖县"||provinceList[i].name=="县"){
  44. }else{
  45. cmbAddOption(cmbArea, provinceList[i].name, provinceList[i]);
  46. }
  47. }
  48. }
  49. cmbSelect(cmbArea, defaultArea);
  50. }
  51. function changeProvince()
  52. {
  53. if(cmbCity==undefined){return false;}
  54. cmbCity.options.length = 0;
  55. cmbCity.onchange = null;
  56. if(cmbProvince.selectedIndex == -1)return;
  57. var item = cmbProvince.options[cmbProvince.selectedIndex].obj;
  58. for(var i=0; i<provinceList.length; i++)
  59. {
  60. if(provinceList[i].pid==item.id){
  61. //if(provinceList[i].name=="市辖区"||provinceList[i].name=="市辖县"||provinceList[i].name=="县"){return false;}
  62. cmbAddOption(cmbCity, provinceList[i].name, provinceList[i]);
  63. }
  64. }
  65. cmbSelect(cmbCity, defaultCity);
  66. changeCity();
  67. cmbCity.onchange = changeCity;
  68. }
  69. function initAreaData() {
  70. try {
  71. xmlHttp = new XMLHttpRequest();
  72. } catch (e) {
  73. try {
  74. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  75. } catch (e) {
  76. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  77. }
  78. }
  79. if (xmlHttp == null) {
  80. alert("浏览器不支持");
  81. return;
  82. }
  83. var url = "/base/js/areadata.js";
  84. xmlHttp.onreadystatechange = stateChanged;
  85. xmlHttp.open("GET", url, true);
  86. xmlHttp.send();
  87. }
  88. function stateChanged (){
  89. if (xmlHttp.readyState == 4) {
  90. provinceList = eval("("+xmlHttp.responseText+")");
  91. for(var i=0; i<provinceList.length; i++)
  92. {
  93. if(cmbProvince==undefined){return false;}
  94. if(provinceList[i].pid==0){
  95. cmbAddOption(cmbProvince, provinceList[i].name, provinceList[i]);
  96. }
  97. }
  98. cmbSelect(cmbProvince, defaultProvince);
  99. changeProvince();
  100. cmbProvince.onchange = changeProvince;
  101. }
  102. }
  103. initAreaData();
  104. }