JavaScript實際應用:簡單二級聯動菜單實現

來源:互聯網
上載者:User
<form name="frm">
<select name="s1" onChange="redirec(document.frm.s1.options.selectedIndex)">
 <option selected>請選擇</option>
 <option value="1">指令碼語言</option>
 <option value="2">進階語言</option>
 <option value="3">其他語言</option>
</select>

<select name="s2">
 <option value="請選擇" selected>請選擇</option>
</select>
</form>


<script language="javascript">
//擷取一級菜單長度
var select1_len = document.frm.s1.options.length;
var select2 = new Array(select1_len);

//把一級菜單都設為數組
for (i=0; i<select1_len; i++) 
...{
 select2[i] = new Array();
}

//定義基本選項
select2[0][0] = new Option("請選擇", " ");

select2[1][0] = new Option("PHP", " ");
select2[1][1] = new Option("ASP", " ");
select2[1][2] = new Option("JSP", " ");

select2[2][0] = new Option("C/C++", " ");
select2[2][1] = new Option("Java", " ");
select2[2][2] = new Option("C#", " ");

select2[3][0] = new Option("Perl", " ");
select2[3][1] = new Option("Ruby", " ");
select2[3][2] = new Option("Python", " ");

//聯動函數
function redirec(x)
...{
 var temp = document.frm.s2; 
 for (i=0;i<select2[x].length;i++)
 ...{
  temp.options[i]=new Option(select2[x][i].text,select2[x][i].value);
 }
 temp.options[0].selected=true;

}

</script>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.