<script language="JavaScript">??
? var onecount;??
? onecount=0;??
? subcat =new Array();?
?
// 建立數組
<%
int count;
String sql0="select * from class ";
ResultSet rs0=RegisterBean.executeQuery(sql0);?? //打開數據庫,這里我用的是javabean鏈接JDBC的方法
count=0;
while(rs0.next())
{
%>????????????????????????????????????????????????????? 小類ID?????????????????????????????????????? 小類名字??????????????????????? 大類ID
subcat[<%=count%>] = new Array("<%=rs0.getString(1)%>","<%=rs0.getString(2)%>","<%=rs0.getString(3)%>");? //給數組賦值,其中rs0.getString(1)代表第1字段,這里不要搞錯字段哦
<%
count=count+1;
}
rs0.close();
%>
onecount=<%=count%>;
function changelocation(id) //實現函數
??? {
??? document.myform.classid.length = 0;
??? var id=id;
?
??? var i;//如果i從1循環那么記錄中第1行的信息就看不到了
??? for (i=0;i<onecount; i++)? //從數組的第1行算起 查找id ,這里的id是你大類的id(下面有我的數組例子)
??????? {
??????????? if (subcat[i][2] ==id)? //第一次循環從第1行,第3列,那1列是大類ID
??????????? {
???document.myform.classid.options[document.myform.classid.length] = new Option(subcat[i][1], subcat[i][0]); //導出數組,導出小類名稱和小類ID
??????????? }???????
??????? }
???????
??? }???
</script>
<form? name="myform" method="post" action="#">
<select name="categoryid"? style="font-size:14px; width:100px;"? size="8" onChange="changelocation(document.myform.categoryid.options[document.myform.categoryid.selectedIndex].value);">
<%
String sql1="select * from category ";
ResultSet rs1=RegisterBean.executeQuery(sql1);
while(rs1.next())
{
%>?
<option value="<%=rs1.getInt(1)%>"><%=rs1.getString(2)%></option>
<%
}
rs1.close();
%>
</select>
<select name="classid" style="font-size:14px;width:100px" size="8">
</select>
例子
0???????????????? 1?????????????????? 2
chassid?? classname?? categoryid
小類ID??? 小類名字????? 大類ID
?備注:?搞清出javascript如何實現很重要