Posted on 2007-04-12 10:21
火星 閱讀(4110)
評(píng)論(2) 編輯 收藏 所屬分類:
JavaScript 、
Java
兩種方案
一個(gè)是用JavaScript,一次性將兩個(gè)select的數(shù)據(jù)統(tǒng)統(tǒng)取出來,將第二張表的數(shù)據(jù)放到數(shù)組中,通過第一個(gè)的change事件來改變,優(yōu)點(diǎn),運(yùn)行速度快,缺點(diǎn),需編寫對(duì)應(yīng)的javascript代碼,且無法反映實(shí)時(shí)數(shù)據(jù)
第二種方案是每次都提交,進(jìn)行聯(lián)動(dòng),優(yōu)點(diǎn),能反映數(shù)據(jù)庫(kù)實(shí)時(shí)數(shù)據(jù)信息,無須寫javascript,缺點(diǎn),運(yùn)行速度相對(duì)慢,且需要考慮保存用戶的已經(jīng)填寫的其他表單數(shù)據(jù)
看我的,方便
<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("數(shù)據(jù)庫(kù)連接失??!");}
%>
</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>