DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery入門技巧 >> 省市區三級聯動jquery實現代碼
省市區三級聯動jquery實現代碼
編輯:JQuery入門技巧     

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

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

     對於第一個問題的解決,其實熟悉Jquery的博友估計是不難的,主要涉及:find,eq,attr等函數的操作。下面是其中涉及到的一個案例:用於獲取省的所有數據,並將其放到下拉框中:

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>"); 
 }); 
 } 

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

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>省市區三級聯動</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> 

     然後注意放進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=\"\">選擇市(區)</option>"; 
  var preArea = "<option value=\"\">選擇區(縣)</option>"; 
   
  //初始化 
  province.html(preProvince); 
  city.html(preCity); 
  area.html(preArea); 
   
  //文檔加載完畢:即從province_city_select_Info.xml獲取數據,成功之後采用 
  //func_suc_getXmlProvice進行 省的 解析 
  $.ajax({ 
    type : "GET", 
    url : "province_city_select_Info.xml", 
    success : func_suc_getXmlProvice 
  }); 
   
  //省 下拉選擇發生變化觸發的事件 
  province.change(function() { 
    //province.val() : 返回是每個省對應的下標,序號從0開始 
    if (province.val() != "") { 
      city.html(preCity); 
       
      //根據下拉得到的省對於的下標序號,動態從從province_city_select_Info.xml獲取數據,成功之後采用 
      //func_suc_getXmlProvice進行省對應的市的解析 
      $.ajax({ 
        type : "GET", 
        url : "province_city_select_Info.xml", 
        success : func_suc_getXmlCity 
      }); 
       
    } 
  }); 
   
  //市 下拉選擇發生變化觸發的事件 
  city.change(function() { 
    area.html(preArea); 
    $.ajax({ 
      type : "GET", 
      url : "province_city_select_Info.xml", 
       
      //根據下拉得到的省、市對於的下標序號,動態從從province_city_select_Info.xml獲取數據,成功之後采用 
      //func_suc_getXmlArea進行省對應的市對於的區的解析 
      success : func_suc_getXmlArea 
    }); 
  }); 
   
  //區 下拉選擇發生變化觸發的事件 
  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裡面的內容啦,因為比較多,我只貼出一部分,如下所示,其余的可以到我這裡來下載:省市區三級數據

<prov id="130000" text="河北省"> 
    <city id="130100" text="石家莊市"> 
      <county id="130102" text="長安區"></county> 
      <county id="130103" text="橋東區"></county> 
      <county id="130104" text="橋西區"></county> 
      <county id="130105" text="新華區"></county> 
      <county id="130107" text="井陉礦區"></county> 
      <county id="130108" text="裕華區"></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="路南區"></county> 
      <county id="130203" text="路北區"></county> 
      <county id="130204" text="古冶區"></county> 
      <county id="130205" text="開平區"></county> 
      <county id="130207" text="豐南區"></county> 
      <county id="130208" text="豐潤區"></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="海港區"></county> 
      <county id="130303" text="山海關區"></county> 
      <county id="130304" text="北戴河區"></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="邯山區"></county> 
      <county id="130403" text="叢台區"></county> 
      <county id="130404" text="復興區"></county> 
      <county id="130406" text="峰峰礦區"></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="肥鄉縣"></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="橋東區"></county> 
      <county id="130503" text="橋西區"></county> 
      <county id="130521" text="邢台縣"></county> 
      <county id="130522" text="臨城縣"></county> 
      <county id="130523" text="內丘縣"></county> 
      <county id="130524" text="柏鄉縣"></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="平鄉縣"></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="新市區"></county> 
      <county id="130603" text="北市區"></county> 
      <county id="130604" text="南市區"></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="橋東區"></county> 
      <county id="130703" text="橋西區"></county> 
      <county id="130705" text="宣化區"></county> 
      <county id="130706" text="下花園區"></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="雙橋區"></county> 
      <county id="130803" text="雙灤區"></county> 
      <county id="130804" text="鷹手營子礦區"></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="新華區"></county> 
      <county id="130903" text="運河區"></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="安次區"></county> 
      <county id="131003" text="廣陽區"></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="桃城區"></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> 

好了,介紹一下效果:

剛開始的:


 

下拉選擇省的,然後出現市的,選擇了市的,然後出現了區的,最後選擇區的時候,得到地址:





以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved