下拉框二级联动

<html>
<body>
 
<select id="first" onChange="change()">
   <option selected="selected">湖北</option>
   <option>广东</option>
</select>
 
<select id="second">
   <option>黄冈</option>
   <option selected="selected">武汉</option>
</select>
 
 
<script>
function change()
{
   var x = document.getElementById("first");
   var y = document.getElementById("second");
   y.options.length = 0; // 清除second下拉框的所有内容
   if(x.selectedIndex == 0)
   {
        y.options.add(new Option("黄冈", "0"));
        y.options.add(new Option("武汉", "1", false, true));  // 默认选中省会城市
   }
 
   if(x.selectedIndex == 1)
   {
        y.options.add(new Option("深圳", "0"));
        y.options.add(new Option("广州", "1", false, true));  // 默认选中省会城市
        y.options.add(new Option("肇庆", "2"));
   }
 
}
</script>
  
</body>
</html>

 

posted @ 2020-10-20 15:51  神经男孩  阅读(183)  评论(0编辑  收藏  举报