jQuery select表单提交省市区城市三级联动核心代码
jquery select表单提交省市区城市三级联动,引用的是“jquery-1.7.min”类库,地区码查询地区名用数组存在areadata_min,下面摘要部分代码:
selectarea.htm文件
. 代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>jquery select表单提交省市区城市三级联动</title>
<script src="jquery-1.7.min.js" type="text/javascript"></script>
<script src="area.js" type="text/javascript"></script>
<script src="areadata_min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function (){
initcomplexarea('seachprov', 'seachcity', 'seachdistrict', area_array, sub_array, '44', '0', '0');
});
//得到地区码
function getareaid(){
var area = 0;
if($("#seachdistrict").val() != "0"){
area = $("#seachdistrict").val();
}else if ($("#seachcity").val() != "0"){
area = $("#seachcity").val();
}else{
area = $("#seachprov").val();
}
return area;
}
function showareaid() {
//地区码
var areaid = getareaid();
//地区名
var areaname = getareanamebyid(areaid) ;
alert("您选择的地区码:" + areaid + " 地区名:" + areaname);
}
//根据地区码查询地区名
function getareanamebyid(areaid){
var areaname = "";
if(areaid.length == 2){
areaname = area_array[areaid];
}else if(areaid.length == 4){
var index1 = areaid.substring(0, 2);
areaname = area_array[index1] + " " + sub_array[index1][areaid];
}else if(areaid.length == 6){
var index1 = areaid.substring(0, 2);
var index2 = areaid.substring(0, 4);
areaname = area_array[index1] + " " + sub_array[index1][index2] + " " + sub_arr[index2][areaid];
}
return areaname;
}
</script>
</head>
<body>
<label>请选择省市地区:</label></br>
<select id="seachprov" name="seachprov" onchange="changecomplexprovince(this.value, sub_array, 'seachcity', 'seachdistrict');"></select>
<select id="seachcity" name="homecity" onchange="changecity(this.value,'seachdistrict','seachdistrict');"></select>
<span id="seachdistrict_p"><select id="seachdistrict" name="seachdistrict"></select></span>
<input type="button" value="获取地区" onclick="showareaid()"/>
</body>
</html>
效果图: