写一个全国所有城市省市区的联调选择器
时间: 2023-06-09 10:03:21 浏览: 97
好的,我可以为您提供一个基于JavaScript的三级联动城市选择器的实现。以下是代码示例:
HTML代码:
```html
<select id="province" onchange="changeProvince()"></select>
<select id="city" onchange="changeCity()"></select>
<select id="district"></select>
```
JavaScript代码:
```javascript
// 声明全国省市区三级联动列表
var cityList = [
{
name: '北京市',
code: '110000',
cityList: [
{
name: '北京市',
code: '110100',
districtList: [
{ name: '东城区', code: '110101' },
{ name: '西城区', code: '110102' },
{ name: '朝阳区', code: '110105' },
{ name: '丰台区', code: '110106' },
{ name: '石景山区', code: '110107' },
{ name: '海淀区', code: '110108' },
{ name: '门头沟区', code: '110109' },
{ name: '房山区', code: '110111' },
{ name: '通州区', code: '110112' },
{ name: '顺义区', code: '110113' },
{ name: '昌平区', code: '110114' },
{ name: '大兴区', code: '110115' },
{ name: '怀柔区', code: '110116' },
{ name: '平谷区', code: '110117' },
{ name: '密云区', code: '110118' },
{ name: '延庆区', code: '110119' },
]
}
]
},
// 省市区数据省略...
];
// 根据省份初始化城市列表
function initCity(provinceCode) {
var citySelect = document.getElementById('city');
var districtSelect = document.getElementById('district');
// 清空城市列表
citySelect.innerHTML = '';
districtSelect.innerHTML = '';
// 根据省份code查找对应的城市列表
var province = cityList.find(item => item.code === provinceCode);
if (province) {
var cityList = province.cityList;
for (var i = 0; i < cityList.length; i++) {
var option = document.createElement('option');
option.value = cityList[i].code;
option.text = cityList[i].name;
citySelect.add(option);
}
// 初始化第一个城市对应的区县
initDistrict(cityList[0].code);
}
}
// 根据城市初始化区县列表
function initDistrict(cityCode) {
var districtSelect = document.getElementById('district');
// 清空区县列表
districtSelect.innerHTML = '';
// 根据城市code查找对应的区县列表
var provinceCode = document.getElementById('province').value;
var province = cityList.find(item => item.code === provinceCode);
if (province) {
var city = province.cityList.find(item => item.code === cityCode);
if (city) {
var districtList = city.districtList;
for (var i = 0; i < districtList.length; i++) {
var option = document.createElement('option');
option.value = districtList[i].code;
option.text = districtList[i].name;
districtSelect.add(option);
}
}
}
}
// 省份选择器变动事件
function changeProvince() {
var provinceSelect = document.getElementById('province');
var provinceCode = provinceSelect.value;
initCity(provinceCode);
}
// 城市选择器变动事件
function changeCity() {
var citySelect = document.getElementById('city');
var cityCode = citySelect.value;
initDistrict(cityCode);
}
// 页面加载时初始化省份列表
function onLoad() {
var provinceSelect = document.getElementById('province');
// 添加空白option作为提示
var option = document.createElement('option');
option.value = '';
option.text = '请选择省份';
provinceSelect.add(option);
// 初始化省份列表
for (var i = 0; i < cityList.length; i++) {
var option = document.createElement('option');
option.value = cityList[i].code;
option.text = cityList[i].name;
provinceSelect.add(option);
}
}
// 页面加载完成后初始化
window.onload = onLoad;
```
以上代码实现了一个基于JavaScript的三级联动城市选择器,可以根据选择的省份、城市来动态显示对应的区县。其中`cityList`数组包含了全国省市区的数据,可以根据需要自行扩展或修改。
阅读全文