省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼
最近項(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中(即下拉框中!)
對(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=\"\">選擇?。ㄊ校?lt;/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="唐??h"></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í)有所幫助,也希望大家多多支持腳本之家。
- 簡(jiǎn)單實(shí)用jquery版三級(jí)聯(lián)動(dòng)select示例
- jquery+json 通用三級(jí)聯(lián)動(dòng)下拉列表
- jQuery select表單提交省市區(qū)城市三級(jí)聯(lián)動(dòng)核心代碼
- jQuery實(shí)現(xiàn)的省市縣三級(jí)聯(lián)動(dòng)菜單效果完整實(shí)例
- jQuery+jsp實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果(附源碼)
- asp.net省市三級(jí)聯(lián)動(dòng)的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- 基于jQuery+JSON的省市二三級(jí)聯(lián)動(dòng)效果
- JSON+Jquery省市區(qū)三級(jí)聯(lián)動(dòng)
- jquery實(shí)現(xiàn)的省市區(qū)三級(jí)聯(lián)動(dòng)
- jQuery實(shí)現(xiàn)簡(jiǎn)單三級(jí)聯(lián)動(dòng)效果
相關(guān)文章
jquery 實(shí)現(xiàn)輪播圖詳解及實(shí)例代碼
這篇文章主要介紹了jquery 實(shí)現(xiàn)輪播圖相關(guān)資料,經(jīng)過(guò)一段時(shí)間學(xué)習(xí)jquery 的知識(shí),這里寫一個(gè)簡(jiǎn)單的輪播圖,需要的朋友可以參考下2016-10-10jQuery超簡(jiǎn)單遮罩層實(shí)現(xiàn)方法示例
這篇文章主要介紹了jQuery超簡(jiǎn)單遮罩層實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了jQuery遮罩層相關(guān)屬性樣式動(dòng)態(tài)控制操作技巧,需要的朋友可以參考下2018-09-09BootStrap輕松實(shí)現(xiàn)微信頁(yè)面開(kāi)發(fā)代碼分享
本文給大家分享一段代碼關(guān)于bootstrap實(shí)現(xiàn)微信開(kāi)發(fā)頁(yè)面的代碼,非常不錯(cuò)代碼簡(jiǎn)單易懂,感興趣的朋友一起看看吧2016-10-10jquery帶翻頁(yè)動(dòng)畫的電子雜志代碼分享
jquery帶翻頁(yè)動(dòng)畫的電子雜志是一款jquery.booklet電子書小冊(cè)子帶翻頁(yè)動(dòng)畫的電子雜志書本,感興趣的小伙伴們可以參考一下2015-08-08JQuery實(shí)現(xiàn)動(dòng)態(tài)添加刪除評(píng)論的方法
這篇文章主要介紹了JQuery實(shí)現(xiàn)動(dòng)態(tài)添加刪除評(píng)論的方法,涉及jQuery處理鼠標(biāo)事件及json數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-05-05jQuery UI AutoComplete 使用說(shuō)明
在 jQuery UI 的最近更新中增加了自動(dòng)完成控件 AutoComplete,這為我們提供了又一個(gè)強(qiáng)大的開(kāi)發(fā)工具,這里介紹一下這個(gè)控件的使用方式。2011-06-06