基于Jquery全国省市区三级联动(含全国省市区数据)

时间:2021-09-11 11:36:51

html

<ul>
<li>
<a>地址</a>
<select id="province">
<option value="">省份</option>
</select>
</li>
<li>
<select id="city">
<option value="">城市</option>
</select>
</li>
<li>
<select id="county">
<option value="">区县</option>
</select>
</li>
</ul>

js:

<script src="${ctx}/js/sitedata_bas.js"></script>
<script>
/**加载省份列表到选择框*/
$("#province").empty();
for(var i=0,length=arrCity.length; i<length; i++){
$("#province").append("<option value='"+arrCity[i].name+"'>"+arrCity[i].name+"</option>");
}

var province;
var city;
var county;

/**省份选择后加载城市数据*/
$("#province").on("change",function(){
console.log("省份:" + $("#province").val());
for(var i=0,length=arrCity.length; i<length; i++){
if($("#province").val()==arrCity[i].name){
city = arrCity[i].sub;
break;
}
}
$("#city").empty();
for(var i=0,length=city.length; i<length; i++){
$("#city").append("<option value='"+city[i].name+"'>"+city[i].name+"</option>");
}
})

/**城市选择后加载区县数据*/
$("#city").on("change",function(){
console.log("城市:"+$("#city").val());
for(var i=0,length=city.length;i<length;i++){
if($("#city").val()==city[i].name){
county = city[i].sub;
break;
}
}
$("#county").empty();
for(var i=0,length=county.length; i<length; i++){
$("#county").append("<option value='"+county[i].name+"'>"+county[i].name+"</option>");
}
})
</script>

sitedata_bas.js

http://download.csdn.net/download/nifury/9602408