省市县联动列表html代码,html+js 全国 省市县三级联动
时间: 2023-10-30 10:09:20 浏览: 105
省市县三级联动包括各级代码,编号
以下是一个简单的省市县三级联动的HTML+JS代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>省市县三级联动</title>
</head>
<body>
<select id="province">
<option value="">请选择省份</option>
</select>
<select id="city">
<option value="">请选择城市</option>
</select>
<select id="county">
<option value="">请选择县区</option>
</select>
<script>
// 定义省市县数据
var data = {
"北京市": {
"市辖区": ["东城区", "西城区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "怀柔区", "平谷区"],
"县": ["密云县", "延庆县"]
},
"上海市": {
"市辖区": ["黄浦区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "闵行区", "宝山区", "嘉定区", "浦东新区", "金山区", "松江区", "青浦区", "奉贤区"],
"县": []
},
"浙江省": {
"杭州市": ["西湖区", "上城区", "下城区", "江干区", "拱墅区", "滨江区", "萧山区", "余杭区", "富阳区", "临安区"],
"宁波市": ["海曙区", "江北区", "北仑区", "镇海区", "鄞州区", "奉化区"],
"温州市": ["鹿城区", "龙湾区", "瓯海区", "洞头区", "瑞安市", "乐清市"],
"嘉兴市": ["南湖区", "秀洲区", "海宁市", "平湖市", "桐乡市"],
"湖州市": ["吴兴区", "南浔区", "长兴县", "安吉县"],
"绍兴市": ["越城区", "柯桥区", "上虞区", "新昌县", "诸暨市"],
"金华市": ["婺城区", "金东区", "浦江县", "武义县", "兰溪市", "义乌市", "东阳市"],
"衢州市": ["柯城区", "衢江区", "常山县", "开化县", "龙游县", "江山市"],
"舟山市": ["定海区", "普陀区", "岱山县", "嵊泗县"],
"台州市": ["椒江区", "黄岩区", "路桥区", "三门县", "天台县", "仙居县", "温岭市", "临海市"],
"丽水市": ["莲都区", "青田县", "缙云县", "遂昌县", "松阳县", "云和县", "庆元县", "景宁畲族自治县", "龙泉市"]
}
};
// 初始化省份下拉列表
var province = document.getElementById("province");
for (var key in data) {
var option = document.createElement("option");
option.innerText = key;
province.appendChild(option);
}
// 初始化城市下拉列表
var city = document.getElementById("city");
province.onchange = function() {
var provinceName = province.value;
city.innerHTML = "<option value=''>请选择城市</option>";
county.innerHTML = "<option value=''>请选择县区</option>";
if (provinceName) {
var cityData = data[provinceName];
for (var key in cityData) {
var option = document.createElement("option");
option.innerText = key;
city.appendChild(option);
}
}
};
// 初始化县区下拉列表
var county = document.getElementById("county");
city.onchange = function() {
var provinceName = province.value;
var cityName = city.value;
county.innerHTML = "<option value=''>请选择县区</option>";
if (provinceName && cityName) {
var countyData = data[provinceName][cityName];
for (var i = 0; i < countyData.length; i++) {
var option = document.createElement("option");
option.innerText = countyData[i];
county.appendChild(option);
}
}
};
</script>
</body>
</html>
```
在这个例子中,我们使用一个JavaScript对象来存储省市县数据,然后使用`select`元素创建下拉列表,通过JavaScript代码实现三级联动的功能。用户选择省份时,会自动加载该省份下的城市列表,选择城市时,会自动加载该城市下的县区列表。
阅读全文