国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼

2019-11-20 08:48:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近項(xiàng)目需要用到關(guān)于省市區(qū)三級(jí)聯(lián)動(dòng)下拉選擇的功能,于是乎網(wǎng)上搜了一些做法,覺(jué)得有一些只是給出了小的案例,卻很難找到詳細(xì)的省、市、區(qū)的具體數(shù)據(jù)(其實(shí),用baidu搜索就是這樣啦),果斷用google,搜出來(lái)的博文質(zhì)量相當(dāng)高,特此記錄記錄?。?!

     對(duì)于這個(gè)效果,其實(shí)我發(fā)現(xiàn)主要在于兩點(diǎn):1、jquery的篩選遍歷操作;2、存儲(chǔ)省、市、區(qū)這些數(shù)據(jù)時(shí)候的格式。另外一點(diǎn)是如何將獲取得到的數(shù)據(jù)放到select option中(即下拉框中?。?br />

     對(duì)于第一個(gè)問(wèn)題的解決,其實(shí)熟悉Jquery的博友估計(jì)是不難的,主要涉及:find,eq,attr等函數(shù)的操作。下面是其中涉及到的一個(gè)案例:用于獲取省的所有數(shù)據(jù),并將其放到下拉框中:

function func_suc_getXmlProvice(xml) {  //jquery的查找功能  var sheng = $(xml).find("prov");    //jquery的遍歷與查詢匹配 eq功能,并將其放到下拉框中  sheng.each(function(i) {   province.append("<option value=" + i + ">"    + sheng.eq(i).attr("text") + "</option>");  });  } 

     下面進(jìn)入正題,建立一個(gè)動(dòng)態(tài)web工程,然后將下面講到的html、js文件、存放省市區(qū)xml文件 都放在Web-Contetn下即可。首先看一看前端html文件province_city_select.html:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>省市區(qū)三級(jí)聯(lián)動(dòng)</title> <script type="text/javascript" src="jquery-1.8.3.min.js"></script> <script type="text/javascript" src="province_city.js"></script>  </head> <body>   <select id="province" name="province">   </select>   <select id="city" name="city">   </select>   <select id="area" name="area">   </select>   <div>     地址是:<span id="pro_city"></span>  <input id="txtProCity" type="text" />   </div> </body> </html> 

     然后注意放進(jìn)jquery-1.8.3.min.js,可以來(lái)我這里下來(lái):jquery庫(kù)文件。然后需要新建province_city.js,其源碼如下:

$(function() {      var address = $("#pro_city");   var province = $("#province");   var city = $("#city");   var area = $("#area");   var preProvince = "<option value=/"/">選擇省(市)</option>";   var preCity = "<option value=/"/">選擇市(區(qū))</option>";   var preArea = "<option value=/"/">選擇區(qū)(縣)</option>";      //初始化   province.html(preProvince);   city.html(preCity);   area.html(preArea);      //文檔加載完畢:即從province_city_select_Info.xml獲取數(shù)據(jù),成功之后采用   //func_suc_getXmlProvice進(jìn)行 省的 解析   $.ajax({     type : "GET",     url : "province_city_select_Info.xml",     success : func_suc_getXmlProvice   });      //省 下拉選擇發(fā)生變化觸發(fā)的事件   province.change(function() {     //province.val() : 返回是每個(gè)省對(duì)應(yīng)的下標(biāo),序號(hào)從0開(kāi)始     if (province.val() != "") {       city.html(preCity);              //根據(jù)下拉得到的省對(duì)于的下標(biāo)序號(hào),動(dòng)態(tài)從從province_city_select_Info.xml獲取數(shù)據(jù),成功之后采用       //func_suc_getXmlProvice進(jìn)行省對(duì)應(yīng)的市的解析       $.ajax({         type : "GET",         url : "province_city_select_Info.xml",         success : func_suc_getXmlCity       });            }   });      //市 下拉選擇發(fā)生變化觸發(fā)的事件   city.change(function() {     area.html(preArea);     $.ajax({       type : "GET",       url : "province_city_select_Info.xml",              //根據(jù)下拉得到的省、市對(duì)于的下標(biāo)序號(hào),動(dòng)態(tài)從從province_city_select_Info.xml獲取數(shù)據(jù),成功之后采用       //func_suc_getXmlArea進(jìn)行省對(duì)應(yīng)的市對(duì)于的區(qū)的解析       success : func_suc_getXmlArea     });   });      //區(qū) 下拉選擇發(fā)生變化觸發(fā)的事件   area.change(function() {     var value = province.find("option:selected").text()         + city.find("option:selected").text()         + area.find("option:selected").text();     address.text(value);     $("#txtProCity").val(value);   });      //解析獲取xml格式文件中的prov標(biāo)簽,得到所有的省,并逐個(gè)進(jìn)行遍歷 放進(jìn)下拉框中   function func_suc_getXmlProvice(xml) {     //jquery的查找功能     var sheng = $(xml).find("prov");          //jquery的遍歷與查詢匹配 eq功能,并將其放到下拉框中     sheng.each(function(i) {       province.append("<option value=" + i + ">"           + sheng.eq(i).attr("text") + "</option>");     });   }      function func_suc_getXmlCity(xml) {     var xml_sheng = $(xml).find("prov");     var pro_num = parseInt(province.val());     var xml_shi = xml_sheng.eq(pro_num).find("city");     xml_shi.each(function(j) {       city.append("<option value=" + j + ">"           + xml_shi.eq(j).attr("text") + "</option>");     });   }      function func_suc_getXmlArea(xml) {     var xml_sheng = $(xml).find("prov");     var pro_num = parseInt(province.val());     var xml_shi = xml_sheng.eq(pro_num).find("city");     var city_num = parseInt(city.val());     var xml_xianqu = xml_shi.eq(city_num).find("county");     xml_xianqu.each(function(k) {       area.append("<option value=" + k + ">"           + xml_xianqu.eq(k).attr("text") + "</option>");     });   } }); 

     然后,重點(diǎn)來(lái)了,當(dāng)然是province_city_select_Info.xml里面的內(nèi)容啦,因?yàn)楸容^多,我只貼出一部分,如下所示,其余的可以到我這里來(lái)下載:省市區(qū)三級(jí)數(shù)據(jù)

<prov id="130000" text="河北省">     <city id="130100" text="石家莊市">       <county id="130102" text="長(zhǎng)安區(qū)"></county>       <county id="130103" text="橋東區(qū)"></county>       <county id="130104" text="橋西區(qū)"></county>       <county id="130105" text="新華區(qū)"></county>       <county id="130107" text="井陘礦區(qū)"></county>       <county id="130108" text="裕華區(qū)"></county>       <county id="130121" text="井陘縣"></county>       <county id="130123" text="正定縣"></county>       <county id="130124" text="欒城縣"></county>       <county id="130125" text="行唐縣"></county>       <county id="130126" text="靈壽縣"></county>       <county id="130127" text="高邑縣"></county>       <county id="130128" text="深澤縣"></county>       <county id="130129" text="贊皇縣"></county>       <county id="130130" text="無(wú)極縣"></county>       <county id="130131" text="平山縣"></county>       <county id="130132" text="元氏縣"></county>       <county id="130133" text="趙縣"></county>       <county id="130181" text="辛集市"></county>       <county id="130182" text="藁城市"></county>       <county id="130183" text="晉州市"></county>       <county id="130184" text="新樂(lè)市"></county>       <county id="130185" text="鹿泉市"></county>     </city>     <city id="130200" text="唐山市">       <county id="130202" text="路南區(qū)"></county>       <county id="130203" text="路北區(qū)"></county>       <county id="130204" text="古冶區(qū)"></county>       <county id="130205" text="開(kāi)平區(qū)"></county>       <county id="130207" text="豐南區(qū)"></county>       <county id="130208" text="豐潤(rùn)區(qū)"></county>       <county id="130223" text="灤縣"></county>       <county id="130224" text="灤南縣"></county>       <county id="130225" text="樂(lè)亭縣"></county>       <county id="130227" text="遷西縣"></county>       <county id="130229" text="玉田縣"></county>       <county id="130230" text="唐海縣"></county>       <county id="130281" text="遵化市"></county>       <county id="130283" text="遷安市"></county>     </city>     <city id="130300" text="秦皇島市">       <county id="130302" text="海港區(qū)"></county>       <county id="130303" text="山海關(guān)區(qū)"></county>       <county id="130304" text="北戴河區(qū)"></county>       <county id="130321" text="青龍滿族自治縣"></county>       <county id="130322" text="昌黎縣"></county>       <county id="130323" text="撫寧縣"></county>       <county id="130324" text="盧龍縣"></county>     </city>     <city id="130400" text="邯鄲市">       <county id="130402" text="邯山區(qū)"></county>       <county id="130403" text="叢臺(tái)區(qū)"></county>       <county id="130404" text="復(fù)興區(qū)"></county>       <county id="130406" text="峰峰礦區(qū)"></county>       <county id="130421" text="邯鄲縣"></county>       <county id="130423" text="臨漳縣"></county>       <county id="130424" text="成安縣"></county>       <county id="130425" text="大名縣"></county>       <county id="130426" text="涉縣"></county>       <county id="130427" text="磁縣"></county>       <county id="130428" text="肥鄉(xiāng)縣"></county>       <county id="130429" text="永年縣"></county>       <county id="130430" text="邱縣"></county>       <county id="130431" text="雞澤縣"></county>       <county id="130432" text="廣平縣"></county>       <county id="130433" text="館陶縣"></county>       <county id="130434" text="魏縣"></county>       <county id="130435" text="曲周縣"></county>       <county id="130481" text="武安市"></county>     </city>     <city id="130500" text="邢臺(tái)市">       <county id="130502" text="橋東區(qū)"></county>       <county id="130503" text="橋西區(qū)"></county>       <county id="130521" text="邢臺(tái)縣"></county>       <county id="130522" text="臨城縣"></county>       <county id="130523" text="內(nèi)丘縣"></county>       <county id="130524" text="柏鄉(xiāng)縣"></county>       <county id="130525" text="隆堯縣"></county>       <county id="130526" text="任縣"></county>       <county id="130527" text="南和縣"></county>       <county id="130528" text="寧晉縣"></county>       <county id="130529" text="巨鹿縣"></county>       <county id="130530" text="新河縣"></county>       <county id="130531" text="廣宗縣"></county>       <county id="130532" text="平鄉(xiāng)縣"></county>       <county id="130533" text="威縣"></county>       <county id="130534" text="清河縣"></county>       <county id="130535" text="臨西縣"></county>       <county id="130581" text="南宮市"></county>       <county id="130582" text="沙河市"></county>     </city>     <city id="130600" text="保定市">       <county id="130602" text="新市區(qū)"></county>       <county id="130603" text="北市區(qū)"></county>       <county id="130604" text="南市區(qū)"></county>       <county id="130621" text="滿城縣"></county>       <county id="130622" text="清苑縣"></county>       <county id="130623" text="淶水縣"></county>       <county id="130624" text="阜平縣"></county>       <county id="130625" text="徐水縣"></county>       <county id="130626" text="定興縣"></county>       <county id="130627" text="唐縣"></county>       <county id="130628" text="高陽(yáng)縣"></county>       <county id="130629" text="容城縣"></county>       <county id="130630" text="淶源縣"></county>       <county id="130631" text="望都縣"></county>       <county id="130632" text="安新縣"></county>       <county id="130633" text="易縣"></county>       <county id="130634" text="曲陽(yáng)縣"></county>       <county id="130635" text="蠡縣"></county>       <county id="130636" text="順平縣"></county>       <county id="130637" text="博野縣"></county>       <county id="130638" text="雄縣"></county>       <county id="130681" text="涿州市"></county>       <county id="130682" text="定州市"></county>       <county id="130683" text="安國(guó)市"></county>       <county id="130684" text="高碑店市"></county>     </city>     <city id="130700" text="張家口市">       <county id="130702" text="橋東區(qū)"></county>       <county id="130703" text="橋西區(qū)"></county>       <county id="130705" text="宣化區(qū)"></county>       <county id="130706" text="下花園區(qū)"></county>       <county id="130721" text="宣化縣"></county>       <county id="130722" text="張北縣"></county>       <county id="130723" text="康保縣"></county>       <county id="130724" text="沽源縣"></county>       <county id="130725" text="尚義縣"></county>       <county id="130726" text="蔚縣"></county>       <county id="130727" text="陽(yáng)原縣"></county>       <county id="130728" text="懷安縣"></county>       <county id="130729" text="萬(wàn)全縣"></county>       <county id="130730" text="懷來(lái)縣"></county>       <county id="130731" text="涿鹿縣"></county>       <county id="130732" text="赤城縣"></county>       <county id="130733" text="崇禮縣"></county>     </city>     <city id="130800" text="承德市">       <county id="130802" text="雙橋區(qū)"></county>       <county id="130803" text="雙灤區(qū)"></county>       <county id="130804" text="鷹手營(yíng)子礦區(qū)"></county>       <county id="130821" text="承德縣"></county>       <county id="130822" text="興隆縣"></county>       <county id="130823" text="平泉縣"></county>       <county id="130824" text="灤平縣"></county>       <county id="130825" text="隆化縣"></county>       <county id="130826" text="豐寧滿族自治縣"></county>       <county id="130827" text="寬城滿族自治縣"></county>       <county id="130828" text="圍場(chǎng)滿族蒙古族自治縣"></county>     </city>     <city id="130900" text="滄州市">       <county id="130902" text="新華區(qū)"></county>       <county id="130903" text="運(yùn)河區(qū)"></county>       <county id="130921" text="滄縣"></county>       <county id="130922" text="青縣"></county>       <county id="130923" text="東光縣"></county>       <county id="130924" text="海興縣"></county>       <county id="130925" text="鹽山縣"></county>       <county id="130926" text="肅寧縣"></county>       <county id="130927" text="南皮縣"></county>       <county id="130928" text="吳橋縣"></county>       <county id="130929" text="獻(xiàn)縣"></county>       <county id="130930" text="孟村回族自治縣"></county>       <county id="130981" text="泊頭市"></county>       <county id="130982" text="任丘市"></county>       <county id="130983" text="黃驊市"></county>       <county id="130984" text="河間市"></county>     </city>     <city id="131000" text="廊坊市">       <county id="131002" text="安次區(qū)"></county>       <county id="131003" text="廣陽(yáng)區(qū)"></county>       <county id="131022" text="固安縣"></county>       <county id="131023" text="永清縣"></county>       <county id="131024" text="香河縣"></county>       <county id="131025" text="大城縣"></county>       <county id="131026" text="文安縣"></county>       <county id="131028" text="大廠回族自治縣"></county>       <county id="131081" text="霸州市"></county>       <county id="131082" text="三河市"></county>     </city>     <city id="131100" text="衡水市">       <county id="131102" text="桃城區(qū)"></county>       <county id="131121" text="棗強(qiáng)縣"></county>       <county id="131122" text="武邑縣"></county>       <county id="131123" text="武強(qiáng)縣"></county>       <county id="131124" text="饒陽(yáng)縣"></county>       <county id="131125" text="安平縣"></county>       <county id="131126" text="故城縣"></county>       <county id="131127" text="景縣"></county>       <county id="131128" text="阜城縣"></county>       <county id="131181" text="冀州市"></county>       <county id="131182" text="深州市"></county>     </city>   </prov> 

好了,介紹一下效果:

剛開(kāi)始的:


 

下拉選擇省的,然后出現(xiàn)市的,選擇了市的,然后出現(xiàn)了區(qū)的,最后選擇區(qū)的時(shí)候,得到地址:





以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 田东县| 菏泽市| 安远县| 琼结县| 五指山市| 罗平县| 星子县| 三门县| 五台县| 桃园市| 临西县| 紫金县| 天台县| 化州市| 阿城市| 错那县| 合肥市| 象州县| 洞口县| 工布江达县| 华宁县| 正定县| 阿合奇县| 玉环县| 安新县| 南溪县| 特克斯县| 平凉市| 荥阳市| 南漳县| 洛宁县| 屯门区| 黄冈市| 东乡族自治县| 西贡区| 宣武区| 湟源县| 双桥区| 嫩江县| 富蕴县| 呼图壁县|