您的位置:首页 > 其它

关于select联动的两种做法

2014-01-16 15:51 267 查看
第一种方法:

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: