area.js 2.8 KB

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