Posted on 2007-04-12 10:21
火星 阅读(4105)
评论(2) 编辑 收藏 所属分类:
JavaScript 、
Java
两种方案
一个是用JavaScript,一次性将两个select的数据统统取出来,将第二张表的数据放到数组中,通过第一个的change事件来改变,优点,运行速度快,缺点,需编写对应的javascript代码,且无法反映实时数据
第二种方案是每次都提交,进行联动,优点,能反映数据库实时数据信息,无须写javascript,缺点,运行速度相对慢,且需要考虑保存用户的已经填写的其他表单数据
看我的,方便
<form name="form1" method="post">
<td width="40%">
<select>
<%
try{
Connection con=j.getConnection();
Statement statement=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from table";
ResultSet r=statement.executeQuery(sql);
while(r.next())
{
%>
<option value=<%=r.getString(1)%>><%=r.getString(1)%></option>
<%}
j.releaseConnection(con);
}catch(SQLException ee){out.print("数据库连接失败!");}
%>
</select>
</td>
</form>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="user" class="com.conn_" scope="page"/>
<%
String las =request.getParameter("la");
String ars =request.getParameter("ar");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
function yjchang(){
document.form2.la.value=document.form1.la.value;
document.form2.submit();
}
</script>
</head>
<body>
<form action="xx_add.jsp" method=post name="form2">
<input type="hidden" name="la" id="la" value="<%=las%>">
<input type="hidden" name="ar" id="ar" value="<%=ars%>">
</form>
<form name="form1" method="post" action="xx_add_do.jsp">
<br/>
<table width="80%" border="0" align="center" cellpadding="1" cellspacing="1" class="table1">
<tr>
<td width="16%" height="40" align="right">部门:</td>
<td height="40">
<select name="la" id="la" style="width:147" onChange="yjchang()">
<option></option>
<%
String la1=request.getParameter("la");
String la="";
ResultSet rs_la=null;
try{
rs_la=user.executeQuery("select branch_id,name from branch");
while(rs_la.next()){
la="";
if(rs_la.getString("branch_id").equals(la1)){
la="selected";
%>
<option value="<%=rs_la.getString("branch_id")%>" <%=la%>><%=rs_la.getString("name")%></option>
<%
}else{
la="";
%>
<option value="<%=rs_la.getString("branch_id")%>"><%=rs_la.getString("name")%></option>
<%}}
}catch( SQLException sqle ){}
catch( Exception e ){}
finally{
try{
user.getClose();
if (rs_la !=null){
rs_la.close();
rs_la=null;
}
}catch( SQLException sqle ){}
}
%>
</select>
</td>
<td height="40" align="right">员工:</td>
<td height="40">
<select name="ar" id="ar" style="width:147">
<option></option>
<%
if(las!=null){
String ar1=request.getParameter("ar");
String ar="";
ResultSet rs_area=null;
try{
rs_area=user.executeQuery("select ygid,name from hyj.area where branch_id='"+las+"'");
while(rs_area.next()){
ar="";
if(rs_area.getString("ygid").equals(ar1)){
ar="selected";
%>
<option value="<%=rs_area.getString("ygid")%>" <%=ar%>><%=rs_area.getString("name")%></option>
<%}else{%>
<option value="<%=rs_area.getString("ygid")%>"><%=rs_area.getString("name")%></option>
<%}}
}catch( SQLException sqle ){}
catch( Exception e ){}
finally{
try{
user.getClose();
if (rs_area !=null){
rs_area.close();
rs_area=null;
}
}catch( SQLException sqle ){}
}
%>
</select>
</td>
</tr>
<tr align="center">
<td height="40" colspan="4">
<input type="Submit" name="tj" value="提交" class="button">
<input type="reset" name="Submit" value="重置" class="button">
</td>
</tr>
</table>
</form>
</body>
</html>