日期:2014-05-18  浏览次数:20708 次

请问下拉框联动问题
我要实现一个基本的二级联动,第一个下拉框是课程,第二个下拉框是教师,要求是选择相应的课程后,教师下拉框刷新为对应的教师,课程和教师数据存储于数据库中,教师和课程的关系有一个表。
  我现在是用Struts做的这个应用,没有做过一个界面的联动功能,希望各位大虾指点,编码思路或者示例代码都可以。谢谢了!

------解决方案--------------------
给你段成功运行过的代码,给你参考下你就明白了

<%
try
{ Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");}
catch (ClassNotFoundException ce)
{System.out.println( "SQLException: "+ce.getMessage());}
try{
Connection con = DriverManager.getConnection( "jdbc:odbc:*** ", " ", " ");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( "select * from Industry_type where Classtype <> 0 order by Classsort asc ");
%>
<script>
var onecount;
onecount=0;
subcat = new Array();
<%
int count = 0;
while(rs.next())
{
int id = rs.getInt( "ID ");
String iname = rs.getString( "Itypename ");
int type = rs.getInt( "Classtype ");
%>
subcat[ <%=count%> ] = new Array( " <%=iname%> ", " <%=type%> ", " <%=id%> ");
<%
count = count + 1;
}
rs.close();
%>
onecount= <%=count%> ;

function changelocation(locationid)
{
document.form1.itype.length = 0;

var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
document.form1.itype.options[document.form1.itype.length] = new Option(subcat[i][0], subcat[i][2]);
}
}

}
</script>


<%
ResultSet rs3 = stmt.executeQuery( "select * from Industry_type where Classtype = '0 ' order by Classsort asc ");
%>
<tr>
<td height= "31 ">
<div align= "left "> 项目所属行业: </div>
</td>
<td>
<div align= "left ">
<select name= "bitype " onchange= "changelocation(document.form1.bitype.options[document.form1.bitype.selectedIndex].value) " size= "1 ">
<option value= " "> 选择大类 </option>
<%

while(rs3.next())
{
%>
<option value= " <%=rs3.getInt( "id ")%> "> <%=rs3.getString( "Itypename ")%> </option>
<%
}
rs3.close();
%>
</select>
&nbsp;
<select name= "itype ">
<option value= " "> 选择小类 </option>
</select>
<%
stmt.close();
con.close();
}
catch (SQLException e)
{ System.out.println( "SQLException: "+e.getMessage());}
%>
</select>
</div> </td>
</tr>