html三级联动下拉菜单
时间: 2023-07-05 11:24:52 浏览: 103
HTML三级联动下拉菜单可以通过JavaScript实现。以下是一个简单的实现方案:
HTML部分:
```html
<select id="province">
<option value="">请选择省份</option>
<option value="1">浙江省</option>
<option value="2">江苏省</option>
</select>
<select id="city">
<option value="">请选择城市</option>
</select>
<select id="area">
<option value="">请选择区县</option>
</select>
```
JavaScript部分:
```javascript
var cityData = {
"1": {
"name": "浙江省",
"city": {
"101": "杭州市",
"102": "宁波市",
"103": "温州市"
}
},
"2": {
"name": "江苏省",
"city": {
"201": "南京市",
"202": "苏州市",
"203": "无锡市"
}
}
};
var province = document.getElementById("province");
var city = document.getElementById("city");
var area = document.getElementById("area");
province.addEventListener("change", function() {
var provinceValue = this.value;
var cityOptions = "<option value=''>请选择城市</option>";
var areaOptions = "<option value=''>请选择区县</option>";
if (provinceValue !== "") {
var cities = cityData[provinceValue]["city"];
for (var code in cities) {
cityOptions += "<option value='" + code + "'>" + cities[code] + "</option>";
}
}
city.innerHTML = cityOptions;
area.innerHTML = areaOptions;
});
city.addEventListener("change", function() {
var cityValue = this.value;
var areaOptions = "<option value=''>请选择区县</option>";
if (cityValue !== "") {
// 根据城市编码获取区县列表
// ...
}
area.innerHTML = areaOptions;
});
```
以上代码实现了一个简单的省市区三级联动下拉菜单。当选择省份时,根据省份编码获取城市列表,并更新城市下拉菜单;当选择城市时,根据城市编码获取区县列表,并更新区县下拉菜单。
阅读全文