Sam钟林森 通过本文主要向大家介绍了jquery省市区三级联动,js省市区三级联动,js实现三级联动,js省市区三级联动插件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
最近项目需要用到关于省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例,却很难找到详细的省、市、区的具体数据(其实,用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="迁安市"

