4个回答
2013-08-03
展开全部
两种方案
一个是用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>
一个是用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>
推荐于2017-12-15
展开全部
这个只能用ajax实现 我那本ajax书上就讲了这个实例 叫二级联动(甚至三级 四级... 都是一样的思路)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考下列代码:
<html>
< head>
< title>多级关联菜单</title>
< meta http-equiv="Content-Type" content="text/html; charset=gb2312">
< head>
< script language="JavaScript">
< !--
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}
function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}
var dsy = new Dsy();
dsy.add("0",["安徽","北京","福建","甘肃","广东","广西","贵州","海南","河北","河南","黑龙江","湖北","湖南","吉林","江苏","江西","辽宁","内蒙古","宁夏","青海","山东","山西","陕西","上海","四川","天津","西藏","新疆","云南","浙江","重庆"]);
dsy.add("0_0",["安庆","蚌埠","巢湖","池州","滁州","阜阳","合肥","淮北","淮南","黄山","六安","马鞍山","宿州","铜陵","芜湖","宣城","亳州"]);
//-->
< /script>
< script language = JavaScript>
< !--
var s=["s1","s2","s3"];
var opt0 = ["-省-","-市-"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
//-->
< /script>
< /head>
< body onload="setup()">
< form name="frm">
< select id="s1"><option>省</option></select>
< select id="s2"><option>市</option></select>
< /form>
< /body>
< /html>
<html>
< head>
< title>多级关联菜单</title>
< meta http-equiv="Content-Type" content="text/html; charset=gb2312">
< head>
< script language="JavaScript">
< !--
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}
function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}
var dsy = new Dsy();
dsy.add("0",["安徽","北京","福建","甘肃","广东","广西","贵州","海南","河北","河南","黑龙江","湖北","湖南","吉林","江苏","江西","辽宁","内蒙古","宁夏","青海","山东","山西","陕西","上海","四川","天津","西藏","新疆","云南","浙江","重庆"]);
dsy.add("0_0",["安庆","蚌埠","巢湖","池州","滁州","阜阳","合肥","淮北","淮南","黄山","六安","马鞍山","宿州","铜陵","芜湖","宣城","亳州"]);
//-->
< /script>
< script language = JavaScript>
< !--
var s=["s1","s2","s3"];
var opt0 = ["-省-","-市-"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
//-->
< /script>
< /head>
< body onload="setup()">
< form name="frm">
< select id="s1"><option>省</option></select>
< select id="s2"><option>市</option></select>
< /form>
< /body>
< /html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-03
展开全部
调下分辨率
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询