Posted on 2009-06-30 23:23
Gavin.lee 閱讀(197)
評論(0) 編輯 收藏 所屬分類:
web 積累(前端 + 后臺)
呵,其實有些時候要求的沒那么嚴格的話,用JS就能搞定了。我從網上找了一段,
<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>
像這種常見的就是省--市--縣等的聯動了。那個網上一大堆,也幸苦那些作者了。