<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>省市区级联选择器</title>
<style>
body {
font-family: 'Microsoft YaHei', sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
.select-group {
display: flex;
gap: 15px;
margin-bottom: 20px;
}
select {
flex: 1;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
background-color: white;
cursor: pointer;
}
select:disabled {
background-color: #f9f9f9;
color: #999;
}
.result {
margin-top: 20px;
padding: 15px;
background-color: #f0f8ff;
border-radius: 4px;
border-left: 4px solid #1890ff;
}
.footer {
margin-top: 30px;
text-align: center;
color: #666;
font-size: 14px;
}
a {
color: #1890ff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>省市区级联选择器</h1>
<div class="select-group">
<select id="province" onchange="loadCities()">
<option value="">-- 请选择省份 --</option>
</select>
<select id="city" onchange="loadDistricts()" disabled>
<option value="">-- 请选择城市 --</option>
</select>
<select id="district" disabled>
<option value="">-- 请选择区县 --</option>
</select>
</div>
<div class="result" id="result">
请选择省市区查看结果...
</div>
</div>
<div class="footer">
</div>
<script>
// 模拟数据 - 实际应用中应该从后端API获取
const regionData = {
"北京市": {
"北京市": ["东城区", "西城区", "朝阳区", "丰台区", "石景山区", "海淀区", "顺义区", "通州区", "大兴区", "房山区", "门头沟区", "昌平区", "平谷区", "密云区", "延庆区"]
},
"上海市": {
"上海市": ["黄浦区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "浦东新区", "闵行区", "宝山区", "嘉定区", "金山区", "松江区", "青浦区", "奉贤区", "崇明区"]
},
"广东省": {
"广州市": ["荔湾区", "越秀区", "海珠区", "天河区", "白云区", "黄埔区", "番禺区", "花都区", "南沙区", "从化区", "增城区"],
"深圳市": ["罗湖区", "福田区", "南山区", "宝安区", "龙岗区", "盐田区", "龙华区", "坪山区", "光明区"],
"珠海市": ["香洲区", "斗门区", "金湾区"],
"汕头市": ["龙湖区", "金平区", "濠江区", "潮阳区", "潮南区", "澄海区", "南澳县"]
},
"江苏省": {
"南京市": ["玄武区", "秦淮区", "建邺区", "鼓楼区", "浦口区", "栖霞区", "雨花台区", "江宁区", "六合区", "溧水区", "高淳区"],
"苏州市": ["姑苏区", "虎丘区", "吴中区", "相城区", "吴江区", "常熟市", "张家港市", "昆山市", "太仓市"],
"无锡市": ["锡山区", "惠山区", "滨湖区", "梁溪区", "新吴区", "江阴市", "宜兴市"]
},
"浙江省": {
"杭州市": ["上城区", "下城区", "江干区", "拱墅区", "西湖区", "滨江区", "萧山区", "余杭区", "富阳区", "临安区", "桐庐县", "淳安县", "建德市"],
"宁波市": ["海曙区", "江北区", "北仑区", "镇海区", "鄞州区", "奉化区", "象山县", "宁海县", "余姚市", "慈溪市"]
}
};
// 初始化省份选择器
const provinceSelect = document.getElementById('province');
const citySelect = document.getElementById('city');
const districtSelect = document.getElementById('district');
const resultDiv = document.getElementById('result');
// 加载省份数据
Object.keys(regionData).forEach(province => {
const option = document.createElement('option');
option.value = province;
option.textContent = province;
provinceSelect.appendChild(option);
});
// 加载城市数据
function loadCities() {
// 重置城市和区县选择器
citySelect.innerHTML = '<option value="">-- 请选择城市 --</option>';
districtSelect.innerHTML = '<option value="">-- 请选择区县 --</option>';
districtSelect.disabled = true;
const selectedProvince = provinceSelect.value;
if (!selectedProvince) {
citySelect.disabled = true;
updateResult();
return;
}
citySelect.disabled = false;
const cities = regionData[selectedProvince];
Object.keys(cities).forEach(city => {
const option = document.createElement('option');
option.value = city;
option.textContent = city;
citySelect.appendChild(option);
});
updateResult();
}
// 加载区县数据
function loadDistricts() {
// 重置区县选择器
districtSelect.innerHTML = '<option value="">-- 请选择区县 --</option>';
const selectedProvince = provinceSelect.value;
const selectedCity = citySelect.value;
if (!selectedProvince || !selectedCity) {
districtSelect.disabled = true;
updateResult();
return;
}
districtSelect.disabled = false;
const districts = regionData[selectedProvince][selectedCity];
districts.forEach(district => {
const option = document.createElement('option');
option.value = district;
option.textContent = district;
districtSelect.appendChild(option);
});
updateResult();
}
// 更新结果显示
function updateResult() {
const province = provinceSelect.value || '未选择';
const city = citySelect.value || '未选择';
const district = districtSelect.value || '未选择';
resultDiv.innerHTML = `
<strong>当前选择:</strong><br>
省份:${province}<br>
城市:${city}<br>
区县:${district}
`;
}
// 为区县选择器添加事件监听
districtSelect.addEventListener('change', updateResult);
</script>
</body>
</html>