关于select联动的两种做法

第一种方法:

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);
      }
}

posted on 2014-01-16 15:51  一青鸟一  阅读(4965)  评论(0编辑  收藏  举报

导航