成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術(shù)教程 ? 省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼
全站頭部文字 我要出現(xiàn)在這里
省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼 754 2024-02-16   

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

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

對于第一個問題的解決,其實熟悉Jquery的博友估計是不難的,主要涉及:find,eq,attr等函數(shù)的操作。下面是其中涉及到的一個案例:用于獲取省的所有數(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>"); 
 }); 
 } 

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

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>省市區(qū)三級聯(lián)動</title> 
<script type="text/javascript" src="http://www.gimoo.net/t/1812/jquery-1.8.3.min.js"></script> 
<script type="text/javascript" src="http://www.gimoo.net/t/1812/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> 

然后注意放進jquery-1.8.3.min.js,可以來我這里下來:jquery庫文件。然后需要新建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進行 省的 解析 
  $.ajax({ 
    type : "GET", 
    url : "province_city_select_Info.xml", 
    success : func_suc_getXmlProvice 
  }); 
   
  //省 下拉選擇發(fā)生變化觸發(fā)的事件 
  province.change(function() { 
    //province.val() : 返回是每個省對應的下標,序號從0開始 
    if (province.val() != "") { 
      city.html(preCity); 
       
      //根據(jù)下拉得到的省對于的下標序號,動態(tài)從從province_city_select_Info.xml獲取數(shù)據(jù),成功之后采用 
      //func_suc_getXmlProvice進行省對應的市的解析 
      $.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ù)下拉得到的省、市對于的下標序號,動態(tài)從從province_city_select_Info.xml獲取數(shù)據(jù),成功之后采用 
      //func_suc_getXmlArea進行省對應的市對于的區(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標簽,得到所有的省,并逐個進行遍歷 放進下拉框中 
  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>"); 
    }); 
  } 
}); 

然后,重點來了,當然是province_city_select_Info.xml里面的內(nèi)容啦,因為比較多,我只貼出一部分,如下所示,其余的可以到我這里來下載:省市區(qū)三級數(shù)據(jù)

<prov id="130000" text="河北省"> 
    <city id="130100" text="石家莊市"> 
      <county id="130102" text="長安區(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="無極縣"></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="新樂市"></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="開平區(qū)"></county> 
      <county id="130207" text="豐南區(qū)"></county> 
      <county id="130208" text="豐潤區(qū)"></county> 
      <county id="130223" text="灤縣"></county> 
      <county id="130224" text="灤南縣"></county> 
      <county id="130225" text="樂亭縣"></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="山海關區(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="叢臺區(qū)"></county> 
      <county id="130404" text="復興區(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="邢臺市"> 
      <county id="130502" text="橋東區(qū)"></county> 
      <county id="130503" text="橋西區(qū)"></county> 
      <county id="130521" text="邢臺縣"></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="高陽縣"></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="曲陽縣"></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="安國市"></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="陽原縣"></county> 
      <county id="130728" text="懷安縣"></county> 
      <county id="130729" text="萬全縣"></county> 
      <county id="130730" text="懷來縣"></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="鷹手營子礦區(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="圍場滿族蒙古族自治縣"></county> 
    </city> 
    <city id="130900" text="滄州市"> 
      <county id="130902" text="新華區(qū)"></county> 
      <county id="130903" text="運河區(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="獻縣"></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="廣陽區(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="棗強縣"></county> 
      <county id="131122" text="武邑縣"></county> 
      <county id="131123" text="武強縣"></county> 
      <county id="131124" text="饒陽縣"></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> 

好了,介紹一下效果:

剛開始的:

查看圖片

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

查看圖片

查看圖片

查看圖片

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持綠夏網(wǎng)。


主站蜘蛛池模板: 牵牛花的生长变化记录| 公共安全教育第一课| 维罗尼卡| 伊人1314| 虞书欣新剧永夜星河免费观看| 阮虔芷个人资料| bb88| 浪漫体质| 养小动物的作文| 我的吸血鬼学姐| 浙江卫视今晚电视节目表| 大海啊故乡钢琴谱| 欧美日韩欧美日韩| 女生宿舍2在线看| 腰带之下| 幼儿园老师锦旗赠言| 棉袜vk| 爱欲1990未删减版播放| 凤凰电视台| 故都的秋ppt| 成龙电影大全 免费播放| 欧美艳星av名字大全| 新人类男友会触电电视剧免费观看全集| 刘慧| 孕妇入院待产包清单| 上门女婿电影完整版免费| 老板5| 潇洒走一回广场舞完整版| 和平精英pc端| 美网直播| 笼中之怒| smc压力表| 金鸳鸯| 嗯啊不要啊啊啊| 想想办法吧爸爸| cctv17农业农村频道在线直播| 被囚禁的女孩大结局| 普通日记200字可抄| 通天长老 电影| 真的爱你中文谐音歌词| 莴笋是发物吗|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103