二級(jí)聯(lián)動(dòng)Jsp+javascript
Posted on 2006-10-17 09:44 小傻 閱讀(1117) 評(píng)論(1) 編輯 收藏 所屬分類: jsp
<script language="JavaScript">??
? var onecount;??
? onecount=0;??
? subcat =new Array();?
?
// 建立數(shù)組
<%
int count;
String sql0="select * from class ";
ResultSet rs0=RegisterBean.executeQuery(sql0);?? //打開(kāi)數(shù)據(jù)庫(kù),這里我用的是javabean鏈接JDBC的方法
count=0;
while(rs0.next())
{
%>????????????????????????????????????????????????????? 小類ID?????????????????????????????????????? 小類名字??????????????????????? 大類ID
subcat[<%=count%>] = new Array("<%=rs0.getString(1)%>","<%=rs0.getString(2)%>","<%=rs0.getString(3)%>");? //給數(shù)組賦值,其中rs0.getString(1)代表第1字段,這里不要搞錯(cuò)字段哦
<%
count=count+1;
}
rs0.close();
%>
onecount=<%=count%>;
function changelocation(id) //實(shí)現(xiàn)函數(shù)
??? {
??? document.myform.classid.length = 0;
??? var id=id;
?
??? var i;//如果i從1循環(huán)那么記錄中第1行的信息就看不到了
??? for (i=0;i<onecount; i++)? //從數(shù)組的第1行算起 查找id ,這里的id是你大類的id(下面有我的數(shù)組例子)
??????? {
??????????? if (subcat[i][2] ==id)? //第一次循環(huán)從第1行,第3列,那1列是大類ID
??????????? {
???document.myform.classid.options[document.myform.classid.length] = new Option(subcat[i][1], subcat[i][0]); //導(dǎo)出數(shù)組,導(dǎo)出小類名稱和小類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如何實(shí)現(xiàn)很重要