三级联动考察的应该是对于数据的处理,只要清楚其中的关系,再多几级都是一样的:
html部分:
<select id="province" onchange="change(this.value);">
<option>请选择省份</option>
</select>
<select id="city" onchange="countyChange(this.value);">
<option>请选择地市</option>
</select>
<select id="county">
<option>请选择县城</option>
</select>
js部分:
for (ele in region){
var op = new Option(ele,ele,false,false); //new Option("文本","值",true,true).后面两个true分别表示默认被选中和有效
//console.log(op);
province.options[province.length] = op;
}
var op = new Option(ele,ele,false,false); //new Option("文本","值",true,true).后面两个true分别表示默认被选中和有效
//console.log(op);
province.options[province.length] = op;
}
var change = function(src){
city.innerHTML = "";
if(src === '请选择省份'){
var op = new Option('请选择地市','请选择地市',false);
city.options[0] = op;
}else{
for (index in region[src]){
//console.log(index);
var op = new Option(index,index,false);
city.options[city.length] = op;
}
}
//记住选择省份的值
provinceName=src;
countyChange(city.value)
}
var countyChange = function(src2){
county.innerHTML = "";
if(src2 === '请选择地市'){
var op = new Option('请选择县城','请选择县城',false);
county.options[0] = op;
}else{
for (index in region[provinceName][src2]){
//console.log(index);
var op = new Option(region[provinceName][src2][index],region[provinceName][src2][index],false);
county.options[county.length] = op;
}
}
}