第一種方法:
function dong(){
var getSheng = document.getElementById("sheng");
var getShi = document.getElementById("shi");
var where = getSheng.selectedIndex;
for(var j=0;j<getShi.length;j++){//清除二級城市的選項
getShi.remove(j);//用的是select的remove方法,j是option的索引值
j--;
}
for(var i=0;i<honglongjiang[where].length;i++){//添加新二級城市選項
var y=document.createElement('option');//先創建的option選項
y.text=honglongjiang[where][i];//然后給option選項text屬性值。
getShi.add(y);//用select的add方法時行添加創建的option
}
}
第二種方法:
function dong(){
var getSheng = document.getElementById("sheng");
var getShi = document.getElementById("shi");
var where = getSheng.selectedIndex;
getShi.length=0;//用select的length長度賦值0來清除下面的option。
for(var i=0;i<chengshi[where].length;i++)//添加新二級城市選項
{
getShi.options[i]=new Option(chengshi[where][i]);//new Option(text,value);
}
}