js三级联动问题,复制百度代码的就不要回答了
展开全部
<!DOCTYPE HTML>
<html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script>
var pros = ["吉林省","黑龙江省","辽宁省"];
var cities = [
[11,12,13],
[21,22,23],
[31,32,33]
];
var areas = [
[
[111,112,113],
[121,122,123],
[131,132,133]
],
[
[211,212,213],
[221,222,223],
[231,232,233]
],
[
[311,312,313],
[321,322,323],
[331,332,333]
]
];
onload = function(){
for(var i=0;i<pros.length;i++){
var opt = document.createElement("option");
opt.innerHTML = pros[i];
pro.appendChild(opt);
}
pro.onchange = function(){
var opts = city.children;
for(var i=1;i<opts.length;i++){
city.removeChild(opts[i]);
i--;
}
opts = area.children;
for(var i=1;i<opts.length;i++){
area.removeChild(opts[i]);
i--;
}
var ind = this.selectedIndex;
var arr = ind > 0 ? cities[ind-1] : [];
for(var i=0;i<arr.length;i++){
var opt = document.createElement("option");
opt.innerHTML = arr[i];
city.appendChild(opt);
}
}
city.onchange = function(){
var opts = area.children;
for(var i=1;i<opts.length;i++){
area.removeChild(opts[i]);
i--;
}
var ind1 = pro.selectedIndex;
var ind2 = this.selectedIndex;
var arr =ind1>0&&ind2>0 ? areas[ind1-1][ind2-1] : [];
for(var i=0;i<arr.length;i++){
var opt = document.createElement("option");
opt.innerHTML = arr[i];
area.appendChild(opt);
}
}
}
</script>
</head>
<body>
<select id="pro">
<option>请选择省</option>
</select>
<select id="city">
<option>请选择市</option>
</select>
<select id="area">
<option>请选择区</option>
</select>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询