创建一级select标签,将标签添加到HTML上
var select = document.createElement('select');
select.setAttribute('id','sel');
document.body.appendChild(select);
创建二级select标签
var select1 = document.createElement('select');
document.body.appendChild(select1);
创建一级请选择option标签
var xuanze = document.createElement('option');
xuanze.innerHTML = '请选择';
select.appendChild(xuanze);
创建二级请选择option标签
var xuanze1 = document.createElement('option');
xuanze1.innerHTML = '请选择';
select1.appendChild(xuanze1);
循环获取每个省,并将名称添加到option上
for(var i = 0;i < city_data.length;i++){
var option = document.createElement('option');
option.innerHTML = city_data[i].city;
select.appendChild(option);
}
获取select的id
var sel = document.getElementById("sel");
获取每个省下面的市
sel.onchange = function(){
select1.options.length = 1;
if(this.selectedIndex - 1 > -1){
for(var i = 0;i < city_data[this.selectedIndex - 1]['area'].length;i++){
var option1 = document.createElement('option');
option1.innerHTML = city_data[this.selectedIndex - 1]['area'][i]; select1.appendChild(option1);
}
}
}
2057

被折叠的 条评论
为什么被折叠?



