html三级联动下拉列表
时间: 2023-10-02 09:10:56 浏览: 334
省市县三级联动下拉列表.docx.zip
可以通过以下的HTML和JavaScript代码实现一个简单的三级联动下拉列表:
```html
<!DOCTYPE html>
<html>
<head>
<title>三级联动下拉列表</title>
</head>
<body>
<label for="province">选择省份:</label>
<select id="province" onchange="loadCities()">
<option value="">请选择</option>
<option value="江苏省">江苏省</option>
<option value="浙江省">浙江省</option>
<option value="广东省">广东省</option>
</select>
<br>
<label for="city">选择城市:</label>
<select id="city" onchange="loadDistricts()">
<option value="">请选择</option>
</select>
<br>
<label for="district">选择区县:</label>
<select id="district">
<option value="">请选择</option>
</select>
<script>
function loadCities() {
var provinceSelect = document.getElementById("province");
var citySelect = document.getElementById("city");
// 清空城市和区县下拉列表
citySelect.innerHTML = "<option value=''>请选择</option>";
document.getElementById("district").innerHTML = "<option value=''>请选择</option>";
if (provinceSelect.value === "江苏省") {
citySelect.innerHTML += "<option value='南京市'>南京市</option>";
citySelect.innerHTML += "<option value='无锡市'>无锡市</option>";
citySelect.innerHTML += "<option value='苏州市'>苏州市</option>";
} else if (provinceSelect.value === "浙江省") {
citySelect.innerHTML += "<option value='杭州市'>杭州市</option>";
citySelect.innerHTML += "<option value='宁波市'>宁波市</option>";
citySelect.innerHTML += "<option value='温州市'>温州市</option>";
} else if (provinceSelect.value === "广东省") {
citySelect.innerHTML += "<option value='广州市'>广州市</option>";
citySelect.innerHTML += "<option value='深圳市'>深圳市</option>";
citySelect.innerHTML += "<option value='珠海市'>珠海市</option>";
}
}
function loadDistricts() {
var citySelect = document.getElementById("city");
var districtSelect = document.getElementById("district");
// 清空区县下拉列表
districtSelect.innerHTML = "<option value=''>请选择</option>";
if (citySelect.value === "南京市") {
districtSelect.innerHTML += "<option value='玄武区'>玄武区</option>";
districtSelect.innerHTML += "<option value='秦淮区'>秦淮区</option>";
districtSelect.innerHTML += "<option value='建邺区'>建邺区</option>";
} else if (citySelect.value === "无锡市") {
districtSelect.innerHTML += "<option value='崇安区'>崇安区</option>";
districtSelect.innerHTML += "<option value='南长区'>南长区</option>";
districtSelect.innerHTML += "<option value='北塘区'>北塘区</option>";
} else if (citySelect.value === "苏州市") {
districtSelect.innerHTML += "<option value='姑苏区'>姑苏区</option>";
districtSelect.innerHTML += "<option value='吴中区'>吴中区</option>";
districtSelect.innerHTML += "<option value='相城区'>相城区</option>";
} else if (citySelect.value === "杭州市") {
districtSelect.innerHTML += "<option value='上城区'>上城区</option>";
districtSelect.innerHTML += "<option value='下城区'>下城区</option>";
districtSelect.innerHTML += "<option value='江干区'>江干区</option>";
} else if (citySelect.value === "宁波市") {
districtSelect.innerHTML += "<option value='海曙区'>海曙区</option>";
districtSelect.innerHTML += "<option value='江东区'>江东区</option>";
districtSelect.innerHTML += "<option value='江北区'>江北区</option>";
} else if (citySelect.value === "温州市") {
districtSelect.innerHTML += "<option value='鹿城区'>鹿城区</option>";
districtSelect.innerHTML += "<option value='龙湾区'>龙湾区</option>";
districtSelect.innerHTML += "<option value='瓯海区'>瓯海区</option>";
} else if (citySelect.value === "广州市") {
districtSelect.innerHTML += "<option value='越秀区'>越秀区</option>";
districtSelect.innerHTML += "<option value='荔湾区'>荔湾区</option>";
districtSelect.innerHTML += "<option value='海珠区'>海珠区</option>";
} else if (citySelect.value === "深圳市") {
districtSelect.innerHTML += "<option value='福田区'>福田区</option>";
districtSelect.innerHTML += "<option value='罗湖区'>罗湖区</option>";
districtSelect.innerHTML += "<option value='南山区'>南山区</option>";
} else if (citySelect.value === "珠海市") {
districtSelect.innerHTML += "<option value='香洲区'>香洲区</option>";
districtSelect.innerHTML += "<option value='斗门区'>斗门区</option>";
districtSelect.innerHTML += "<option value='金湾区'>金湾区</option>";
}
}
</script>
</body>
</html>
```
这段代码实现了一个简单的三级联动下拉列表,通过选择省份、城市和区县来实现级联效果。
阅读全文