﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-火星的森林-随笔分类-JavaScript</title><link>http://www.blogjava.net/marsforest/category/13006.html</link><description>欢迎到火星来</description><language>zh-cn</language><lastBuildDate>Thu, 12 Apr 2007 04:33:31 GMT</lastBuildDate><pubDate>Thu, 12 Apr 2007 04:33:31 GMT</pubDate><ttl>60</ttl><item><title>JSP下拉框联动如何实现(zt)</title><link>http://www.blogjava.net/marsforest/archive/2007/04/12/110092.html</link><dc:creator>火星的森林</dc:creator><author>火星的森林</author><pubDate>Thu, 12 Apr 2007 02:21:00 GMT</pubDate><guid>http://www.blogjava.net/marsforest/archive/2007/04/12/110092.html</guid><wfw:comment>http://www.blogjava.net/marsforest/comments/110092.html</wfw:comment><comments>http://www.blogjava.net/marsforest/archive/2007/04/12/110092.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/marsforest/comments/commentRss/110092.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/marsforest/services/trackbacks/110092.html</trackback:ping><description><![CDATA[两种方案 &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; 一个是用JavaScript，一次性将两个select的数据统统取出来，将第二张表的数据放到数组中，通过第一个的change事件来改变，优点，运行速度快，缺点，需编写对应的javascript代码，且无法反映实时数据 &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; 第二种方案是每次都提交，进行联动，优点，能反映数据库实时数据信息，无须写javascript,缺点，运行速度相对慢，且需要考虑保存用户的已经填写的其他表单数据 &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; 看我的，方便 &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &lt;form &nbsp; name="form1" &nbsp; method="post"&gt; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;td &nbsp; width="40%"&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;select&gt; &nbsp; <br>&nbsp; &lt;% &nbsp; <br>&nbsp; try{ &nbsp; <br>&nbsp; Connection &nbsp; con=j.getConnection(); &nbsp; <br>&nbsp; Statement &nbsp; statement=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); &nbsp; <br>&nbsp; String &nbsp; sql="select &nbsp; * &nbsp; from &nbsp; table"; &nbsp; <br>&nbsp; ResultSet &nbsp; r=statement.executeQuery(sql); &nbsp; <br>&nbsp; while(r.next()) &nbsp; <br>&nbsp; { &nbsp; <br>&nbsp; %&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;option &nbsp; value=&lt;%=r.getString(1)%&gt;&gt;&lt;%=r.getString(1)%&gt;&lt;/option&gt; &nbsp; <br>&nbsp; &lt;%} &nbsp; <br>&nbsp; j.releaseConnection(con); &nbsp; <br>&nbsp; }catch(SQLException &nbsp; ee){out.print("数据库连接失败！");} &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; %&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/select&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/td&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &lt;/form&gt;<br><br>&lt;%@ &nbsp; page &nbsp; contentType="text/html;charset=gb2312"%&gt; &nbsp; &nbsp; <br>&nbsp; &lt;%@ &nbsp; page &nbsp; import="java.sql.*"%&gt; &nbsp; <br>&nbsp; &lt;%@ &nbsp; page &nbsp; import="java.io.*"%&gt; &nbsp; <br>&nbsp; &lt;jsp:useBean &nbsp; id="user" &nbsp; class="com.conn_" &nbsp; scope="page"/&gt; &nbsp; <br>&nbsp; &lt;% &nbsp; <br>&nbsp; String &nbsp; las &nbsp; =request.getParameter("la"); &nbsp; <br>&nbsp; String &nbsp; ars &nbsp; =request.getParameter("ar"); &nbsp; <br>&nbsp; %&gt; &nbsp; <br>&nbsp; &lt;html&gt; &nbsp; <br>&nbsp; &lt;head&gt; &nbsp; <br>&nbsp; &lt;meta &nbsp; http-equiv="Content-Type" &nbsp; content="text/html; &nbsp; charset=gb2312"&gt; &nbsp; <br>&nbsp; &lt;script &nbsp; language="javascript"&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function &nbsp; yjchang(){ &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; document.form2.la.value=document.form1.la.value; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.form2.submit(); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; <br>&nbsp; &lt;/script&gt; &nbsp; <br>&nbsp; &lt;/head&gt; &nbsp; <br>&nbsp; &lt;body&gt; &nbsp; <br>&nbsp; &lt;form &nbsp; action="xx_add.jsp" &nbsp; method=post &nbsp; name="form2"&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input &nbsp; type="hidden" &nbsp; name="la" &nbsp; id="la" &nbsp; value="&lt;%=las%&gt;"&gt; &nbsp; <br>&nbsp; &lt;input &nbsp; type="hidden" &nbsp; name="ar" &nbsp; id="ar" &nbsp; value="&lt;%=ars%&gt;"&gt; &nbsp; <br>&nbsp; &lt;/form&gt; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; &lt;form &nbsp; name="form1" &nbsp; method="post" &nbsp; action="xx_add_do.jsp"&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &lt;br/&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &lt;table &nbsp; width="80%" &nbsp; &nbsp; border="0" &nbsp; align="center" &nbsp; cellpadding="1" &nbsp; cellspacing="1" &nbsp; class="table1"&gt; &nbsp; <br>&nbsp; &nbsp; &lt;tr&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td &nbsp; width="16%" &nbsp; height="40" &nbsp; align="right"&gt;部门：&lt;/td&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td &nbsp; height="40"&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;select &nbsp; name="la" &nbsp; id="la" &nbsp; style="width:147" &nbsp; onChange="yjchang()"&gt; &nbsp; <br>&nbsp; &lt;option&gt;&lt;/option&gt; &nbsp; <br>&nbsp; &lt;% &nbsp; <br>&nbsp; &nbsp; &nbsp; String &nbsp; la1=request.getParameter("la"); &nbsp; <br>&nbsp; String &nbsp; la=""; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; ResultSet &nbsp; rs_la=null; &nbsp; <br>&nbsp; &nbsp; &nbsp; try{ &nbsp; <br>&nbsp; &nbsp; &nbsp; rs_la=user.executeQuery("select &nbsp; branch_id,name &nbsp; from &nbsp; branch"); &nbsp; <br>&nbsp; while(rs_la.next()){ &nbsp; <br>&nbsp; la=""; &nbsp; <br>&nbsp; if(rs_la.getString("branch_id").equals(la1)){ &nbsp; <br>&nbsp; la="selected"; &nbsp; <br>&nbsp; %&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;option &nbsp; value="&lt;%=rs_la.getString("branch_id")%&gt;" &nbsp; &lt;%=la%&gt;&gt;&lt;%=rs_la.getString("name")%&gt;&lt;/option&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;% &nbsp; <br>&nbsp; }else{ &nbsp; <br>&nbsp; &nbsp; &nbsp; la=""; &nbsp; <br>&nbsp; &nbsp; &nbsp; %&gt; &nbsp; <br>&nbsp; &lt;option &nbsp; value="&lt;%=rs_la.getString("branch_id")%&gt;"&gt;&lt;%=rs_la.getString("name")%&gt;&lt;/option&gt; &nbsp; <br>&nbsp; &lt;%}} &nbsp; <br>&nbsp; }catch( &nbsp; SQLException &nbsp; sqle &nbsp; ){} &nbsp; <br>&nbsp; catch( &nbsp; Exception &nbsp; e &nbsp; ){} &nbsp; <br>&nbsp; finally{ &nbsp; <br>&nbsp; try{ &nbsp; <br>&nbsp; user.getClose(); &nbsp; <br>&nbsp; if &nbsp; (rs_la &nbsp; !=null){ &nbsp; <br>&nbsp; rs_la.close(); &nbsp; <br>&nbsp; rs_la=null; &nbsp; <br>&nbsp; } &nbsp; <br>&nbsp; }catch( &nbsp; SQLException &nbsp; sqle &nbsp; ){} &nbsp; &nbsp; <br>&nbsp; } &nbsp; <br>&nbsp; %&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/select&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/td&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td &nbsp; height="40" &nbsp; align="right"&gt;员工：&lt;/td&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td &nbsp; height="40"&gt; &nbsp; <br>&nbsp; &lt;select &nbsp; name="ar" &nbsp; id="ar" &nbsp; style="width:147"&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;option&gt;&lt;/option&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;% &nbsp; <br>&nbsp; if(las!=null){ &nbsp; <br>&nbsp; String &nbsp; ar1=request.getParameter("ar"); &nbsp; <br>&nbsp; String &nbsp; ar=""; &nbsp; <br>&nbsp; ResultSet &nbsp; rs_area=null; &nbsp; <br>&nbsp; try{ &nbsp; <br>&nbsp; rs_area=user.executeQuery("select &nbsp; ygid,name &nbsp; from &nbsp; hyj.area &nbsp; where &nbsp; branch_id='"+las+"'"); &nbsp; <br>&nbsp; while(rs_area.next()){ &nbsp; <br>&nbsp; &nbsp; ar=""; &nbsp; <br>&nbsp; &nbsp; if(rs_area.getString("ygid").equals(ar1)){ &nbsp; <br>&nbsp; &nbsp; ar="selected"; &nbsp; <br>&nbsp; %&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;option &nbsp; value="&lt;%=rs_area.getString("ygid")%&gt;" &nbsp; &lt;%=ar%&gt;&gt;&lt;%=rs_area.getString("name")%&gt;&lt;/option&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;%}else{%&gt; &nbsp; <br>&nbsp; &lt;option &nbsp; value="&lt;%=rs_area.getString("ygid")%&gt;"&gt;&lt;%=rs_area.getString("name")%&gt;&lt;/option&gt; &nbsp; <br>&nbsp; &lt;%}} &nbsp; <br>&nbsp; }catch( &nbsp; SQLException &nbsp; sqle &nbsp; ){} &nbsp; <br>&nbsp; catch( &nbsp; Exception &nbsp; e &nbsp; ){} &nbsp; <br>&nbsp; finally{ &nbsp; <br>&nbsp; try{ &nbsp; <br>&nbsp; user.getClose(); &nbsp; <br>&nbsp; if &nbsp; (rs_area &nbsp; !=null){ &nbsp; <br>&nbsp; rs_area.close(); &nbsp; <br>&nbsp; rs_area=null; &nbsp; <br>&nbsp; } &nbsp; <br>&nbsp; }catch( &nbsp; SQLException &nbsp; sqle &nbsp; ){} &nbsp; &nbsp; <br>&nbsp; } &nbsp; <br>&nbsp; %&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/select&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &lt;/td&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/tr&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tr &nbsp; align="center"&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td &nbsp; height="40" &nbsp; colspan="4"&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &lt;input &nbsp; type="Submit" &nbsp; name="tj" &nbsp; value="提交" &nbsp; class="button"&gt; &nbsp; <br>&nbsp; &amp;nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input &nbsp; type="reset" &nbsp; name="Submit" &nbsp; value="重置" &nbsp; class="button"&gt; &nbsp; <br>&nbsp; &lt;/td&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/tr&gt; &nbsp; <br>&nbsp; &nbsp; &nbsp; &lt;/table&gt; &nbsp; <br>&nbsp; &lt;/form&gt; &nbsp; <br>&nbsp; &lt;/body&gt; &nbsp; <br>&nbsp; &lt;/html&gt;&nbsp;&nbsp; <br>
<img src ="http://www.blogjava.net/marsforest/aggbug/110092.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/marsforest/" target="_blank">火星的森林</a> 2007-04-12 10:21 <a href="http://www.blogjava.net/marsforest/archive/2007/04/12/110092.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>