最近項目需要用到省、市、區三級聯動的下拉選擇功能,於是上網搜了一些做法。有些只給出了簡單案例,卻很難找到完整的省、市、區數據,於是改用搜尋引擎查找,收穫頗豐,特此記錄。
這個效果其實主要在於兩點:一是 jQuery 的篩選遍歷操作;二是存儲省、市、區數據時的格式。此外還需要解決如何把取得的資料放進 select 的 option 中。
對於第一點,熟悉 jQuery 的朋友應該不難,主要涉及 find、eq、attr 等函數。下面以取得所有省並放入下拉框為例:
function func_suc_getXmlProvice(xml) {
var sheng = $(xml).find("prov"); // jQuery 的查找功能
sheng.each(function(i) { // 遍歷並放入下拉框
province.append("<option value=" + i + ">" + sheng.eq(i).attr("text") + "</option>");
});
}
建立一個動態 web 工程,把 html、js 與存放省市區的 xml 檔案都放在 Web-Content 下即可。先來看前端 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>
接著新建 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);
// 文檔加載完畢:從 xml 獲取數據,成功後解析省
$.ajax({
type : "GET",
url : "province_city_select_Info.xml",
success : func_suc_getXmlProvice
});
// 省 下拉變化事件
province.change(function() {
if (province.val() != "") {
city.html(preCity);
$.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",
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);
});
// 解析 prov 標簽,得到所有省並放入下拉框
function func_suc_getXmlProvice(xml) {
var sheng = $(xml).find("prov");
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="130123" text="正定縣"></county>
<!-- 其餘区县省略 -->
</city>
<city id="130200" text="唐山市">
<county id="130202" text="路南區"></county>
<county id="130203" text="路北區"></county>
<!-- 其餘区县省略 -->
</city>
<!-- 其餘地級市省略 -->
</prov>
效果展示:

選擇省之後出現市,選擇市之後出現區,最後選好區時即可得到完整地址:



整體思路是:以 XML 存儲省、市、區的層級資料,透過 jQuery 的 find、eq、attr 逐層篩選,並在每次 change 事件中重新請求、解析後填充對應的下拉框,最後拼接成完整地址。
以上就是本文的全部內容,希望對大家的學習有所幫助。