js省市級聯--避免id衝突
阿新 • • 發佈:2019-01-07
之前在做專案,使用到省市級聯,但是那只是使用了網上下載的方法,之之後就遇上了,多個地方使用,出現了id衝突的不可避免的情況,專案期間由於沒有時間去檢視那麼多的程式碼,所以都使用了最笨的方法解決了。但是這幾天我重新寫了一遍,用自己最“人性化”的一面去編寫,得到了目前這樣的結果:
(目的就是為了防止id衝突的再次出現)
直接看程式碼:
var parseCityXml = function(province,city,area){
var xmlDoc = null;
//瀏覽器相容 window.DOMParser非ie瀏覽器,,讀取xml檔案
if(window.DOMParser && !window.ActiveXObject){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', 'XMLFile/city.xml', false);
xmlHttp.send(null);
if(xmlHttp.readyState==4){
xmlDoc = xmlHttp.responseXML.documentElement;
}
}else {
xmlDoc = xmlDom();
xmlDoc.async = false;
xmlDoc.load('XMLFile/city.xml');
}
//獲取對應的select
var province = document.getElementById(province);
var city = document.getElementById(city);
var area = document.getElementById(area);
//生成省份的
var xmlProvince = xmlDoc.getElementsByTagName('province');
for(var i=0;i<xmlProvince.length;i++){
addOption(xmlProvince[i],province);
}
//一開始生成城市的
var xmlCity = xmlProvince[0].getElementsByTagName('city');
addOption(xmlCity[0], city);
//一開始生成區域
var xmlArea = xmlCity[0].getElementsByTagName('area');
addOption(xmlArea[0], area);
//為了使得改變城市的時候方便判斷是哪一個省市,否則將迴圈判斷,,浪費資源
//省份開始變化
province.onchange = function(){
//執行前先使得下拉框長度為1;
city.length = 1;
area.length = 1;
//獲取變化的值的value
var provinceValue = province.value;
//獲取判斷成功返回的元素資訊,,用於迴圈新增option元素
var changeXmlProvinceNode = judgeInformationFactory(xmlProvince,provinceValue);
if(xmlProvince[0].getAttribute('postcode') != provinceValue){
var changeXmlCitys = changeXmlProvinceNode.getElementsByTagName('city');
for(var i=0; i<changeXmlCitys.length;i++){
addOption(changeXmlCitys[i], city);
}
}else{
return;
}
}
//城市改變
city.onchange = function(){
//執行前先使得下拉框長度為1;
area.length = 1;
var cityValue = city.value;
if(xmlCity[0].getAttribute('postcode') != cityValue){
var changeXmlProvinceNode = judgeInformationFactory(xmlProvince,province.value);
//獲取判斷成功返回的元素資訊,,用於迴圈新增option元素
var changeXmlCityNode = judgeInformationFactory(changeXmlProvinceNode.getElementsByTagName('city'),cityValue);
var changeXmlAreas = changeXmlCityNode.getElementsByTagName('area');
for(var i=1; i<changeXmlAreas.length;i++){
addOption(changeXmlAreas[i], area);
}
}else{
return;
}
}
}
是不是很簡單?
這都是使用了javaScript對XML檔案的解析
這裡給你們XML檔案:
(不知道從那裡下載了,希望原創不要怪罪小弟,如有侵犯您的權益,請聯絡小弟,小弟親自向您道歉並刪除本篇部落格)
<?xml version="1.0" encoding="UTF-8"?>
<root name="中國">
<province name="--請選擇省份--" postcode="100000" >
<city name="--請選擇城市--" postcode="100100" >
<area name="--請選擇地區--" postcode="100101" />
</city>
</province>
<province name="北京市" postcode="110000" >
<city name="市轄區" postcode="110100" >
<area name="東城區" postcode="110101" />
<area name="西城區" postcode="110102" />
<area name="崇文區" postcode="110103" />
<area name="宣武區" postcode="110104" />
<area name="朝陽區" postcode="110105" />
<area name="豐臺區" postcode="110106" />
<area name="石景山區" postcode="110107" />
<area name="海淀區" postcode="110108" />
<area name="門頭溝區" postcode="110109" />
<area name="房山區" postcode="110111" />
<area name="通州區" postcode="110112" />
<area name="順義區" postcode="110113" />
<area name="昌平區" postcode="110114" />
<area name="大興區" postcode="110115" />
<area name="懷柔區" postcode="110116" />
<area name="平谷區" postcode="110117" />
</city>
<city name="縣" postcode="110200" >
<area name="密雲縣" postcode="110228" />
<area name="延慶縣" postcode="110229" />
</city>
</province>
<province name="天津市" postcode="120000" >
<city name="市轄區" postcode="120100" >
<area name="和平區" postcode="120101" />
<area name="河東區" postcode="120102" />
<area name="河西區" postcode="120103" />
<area name="南開區" postcode="120104" />
<area name="河北區" postcode="120105" />
<area name="紅橋區" postcode="120106" />
<area name="塘沽區" postcode="120107" />
<area name="漢沽區" postcode="120108" />
<area name="大港區" postcode="120109" />
<area name="東麗區" postcode="120110" />
<area name="西青區" postcode="120111" />
<area name="津南區" postcode="120112" />
<area name="北辰區" postcode="120113" />
<area name="武清區" postcode="120114" />
<area name="寶坻區" postcode="120115" />
</city>
<city name="縣" postcode="120200" >
<area name="寧河縣" postcode="120221" />
<area name="靜海縣" postcode="120223" />
<area name="薊縣" postcode="120225" />
</city>
</province>
<province name="河北省" postcode="130000" >
<city name="石家莊市" postcode="130100" >
<area name="長安區" postcode="130102" />
<area name="橋東區" postcode="130103" />
<area name="橋西區" postcode="130104" />
<area name="新華區" postcode="130105" />
<area name="井陘礦區" postcode="130107" />
<area name="裕華區" postcode="130108" />
<area name="井陘縣" postcode="130121" />
<area name="正定縣" postcode="130123" />
<area name="欒城縣" postcode="130124" />
<area name="行唐縣" postcode="130125" />
<area name="靈壽縣" postcode="130126" />
<area name="高邑縣" postcode="130127" />
<area name="深澤縣" postcode="130128" />
<area name="贊皇縣" postcode="130129" />
<area name="無極縣" postcode="130130" />
<area name="平山縣" postcode="130131" />
<area name="元氏縣" postcode="130132" />
<area name="趙縣" postcode="130133" />
<area name="辛集市" postcode="130181" />
<area name="藁城市" postcode="130182" />
<area name="晉州市" postcode="130183" />
<area name="新樂市" postcode="130184" />
<area name="鹿泉市" postcode="130185" />
</city>
<city name="唐山市" postcode="130200" >
<area name="路南區" postcode="130202" />
<area name="路北區" postcode="130203" />
<area name="古冶區" postcode="130204" />
<area name="開平區" postcode="130205" />
<area name="豐南區" postcode="130207" />
<area name="豐潤區" postcode="130208" />
<area name="灤縣" postcode="130223" />
<area name="灤南縣" postcode="130224" />
<area name="樂亭縣" postcode="130225" />
<area name="遷西縣" postcode="130227" />
<area name="玉田縣" postcode="130229" />
<area name="唐海縣" postcode="130230" />
<area name="遵化市" postcode="130281" />
<area name="遷安市" postcode="130283" />
</city>
<city name="秦皇島市" postcode="130300" >
<area name="海港區" postcode="130302" />
<area name="山海關區" postcode="130303" />
<area name="北戴河區" postcode="130304" />
<area name="青龍滿族自治縣" postcode="130321" />
<area name="昌黎縣" postcode="130322" />
<area name="撫寧縣" postcode="130323" />
<area name="盧龍縣" postcode="130324" />
</city>
<city name="邯鄲市" postcode="130400" >
<area name="邯山區" postcode="130402" />
<area name="叢臺區" postcode="130403" />
<area name="復興區" postcode="130404" />
<area name="峰峰礦區" postcode="130406" />
<area name="邯鄲縣" postcode="130421" />
<area name="臨漳縣" postcode="130423" />
<area name="成安縣" postcode="130424" />
<area name="大名縣" postcode="130425" />
<area name="涉縣" postcode="130426" />
<area name="磁縣" postcode="130427" />
<area name="肥鄉縣" postcode="130428" />
<area name="永年縣" postcode="130429" />
<area name="邱縣" postcode="130430" />
<area name="雞澤縣" postcode="130431" />
<area name="廣平縣" postcode="130432" />
<area name="館陶縣" postcode="130433" />
<area name="魏縣" postcode="130434" />
<area name="曲周縣" postcode="130435" />
<area name="武安市" postcode="130481" />
</city>
<city name="邢臺市" postcode="130500" >
<area name="橋東區" postcode="130502" />
<area name="橋西區" postcode="130503" />
<area name="邢臺縣" postcode="130521" />
<area name="臨城縣" postcode="130522" />
<area name="內丘縣" postcode="130523" />
<area name="柏鄉縣" postcode="130524" />
<area name="隆堯縣" postcode="130525" />
<area name="任縣" postcode="130526" />
<area name="南和縣" postcode="130527" />
<area name="寧晉縣" postcode="130528" />
<area name="鉅鹿縣" postcode="130529" />
<area name="新河縣" postcode="130530" />
<area name="廣宗縣" postcode="130531" />
<area name="平鄉縣" postcode="130532" />
<area name="威縣" postcode="130533" />
<area name="清河縣" postcode="130534" />
<area name="臨西縣" postcode="130535" />
<area name="南宮市" postcode="130581" />
<area name="沙河市" postcode="130582" />
</city>
<city name="保定市" postcode="130600" >
<area name="新市區" postcode="130602" />
<area name="北市區" postcode="130603" />
<area name="南市區" postcode="130604" />
<area name="滿城縣" postcode="130621" />
<area name="清苑縣" postcode="130622" />
<area name="淶水縣" postcode="130623" />
<area name="阜平縣" postcode="130624" />
<area name="徐水縣" postcode="130625" />
<area name="定興縣" postcode="130626" />
<area name="唐縣" postcode="130627" />
<area name="高陽縣" postcode="130628" />
<area name="容城縣" postcode="130629" />
<area name="淶源縣" postcode="130630" />
<area name="望都縣" postcode="130631" />
<area name="安新縣" postcode="130632" />
<area name="易縣" postcode="130633" />
<area name="曲陽縣" postcode="130634" />
<area name="蠡縣" postcode="130635" />
<area name="順平縣" postcode="130636" />
<area name="博野縣" postcode="130637" />
<area name="雄縣" postcode="130638" />
<area name="涿州市" postcode="130681" />
<area name="定州市" postcode="130682" />
<area name="安國市" postcode="130683" />
<area name="高碑店市" postcode="130684" />
</city>
<city name="張家口市" postcode="130700" >
<area name="橋東區" postcode="130702" />
<area name="橋西區" postcode="130703" />
<area name="宣化區" postcode="130705" />
<area name="下花園區" postcode="130706" />
<area name="宣化縣" postcode="130721" />
<area name="張北縣" postcode="130722" />
<area name="康保縣" postcode="130723" />
<area name="沽源縣" postcode="130724" />
<area name="尚義縣" postcode="130725" />
<area name="蔚縣" postcode="130726" />
<area name="陽原縣" postcode="130727" />
<area name="懷安縣" postcode="130728" />
<area name="萬全縣" postcode="130729" />
<area name="懷來縣" postcode="130730" />
<area name="涿鹿縣" postcode="130731" />
<area name="赤城縣" postcode="130732" />
<area name="崇禮縣" postcode="130733" />
</city>
<city name="承德市" postcode="130800" >
<area name="雙橋區" postcode="130802" />
<area name="雙灤區" postcode="130803" />
<area name="鷹手營子礦區" postcode="130804" />
<area name="承德縣" postcode="130821" />
<area name="興隆縣" postcode="130822" />
<area name="平泉縣" postcode="130823" />
<area name="灤平縣" postcode="130824" />
<area name="隆化縣" postcode="130825" />
<area name="豐寧滿族自治縣" postcode="130826" />
<area name="寬城滿族自治縣" postcode="130827" />
<area name="圍場滿族蒙古族自治縣" postcode="130828" />
</city>
<city name="滄州市" postcode="130900" >
<area name="新華區" postcode="130902" />
<area name="運河區" postcode="130903" />
<area name="滄縣" postcode="130921" />
<area name="青縣" postcode="130922" />
<area name="東光縣" postcode="130923" />
<area name="海興縣" postcode="130924" />
<area name="鹽山縣" postcode="130925" />
<area name="肅寧縣" postcode="130926" />
<area name="南皮縣" postcode="130927" />
<area name="吳橋縣" postcode="130928" />
<area name="獻縣" postcode="130929" />
<area name="孟村回族自治縣" postcode="130930" />
<area name="泊頭市" postcode="130981" />
<area name="任丘市" postcode="130982" />
<area name="黃驊市" postcode="130983" />
<area name="河間市" postcode="130984" />
</city>
<city name="廊坊市" postcode="131000" >
<area name="安次區" postcode="131002" />
<area name="廣陽區" postcode="131003" />
<area name="固安縣" postcode="131022" />
<area name="永清縣" postcode="131023" />
<area name="香河縣" postcode="131024" />
<area name="大城縣" postcode="131025" />
<area name="文安縣" postcode="131026" />
<area name="大廠回族自治縣" postcode="131028" />
<area name="霸州市" postcode="131081" />
<area name="三河市" postcode="131082" />
</city>
<city name="衡水市" postcode="131100" >
<area name="桃城區" postcode="131102" />
<area name="棗強縣" postcode="131121" />
<area name="武邑縣" postcode="131122" />
<area name="武強縣" postcode="131123" />
<area name="饒陽縣"