有两张表:
表1(Department):DepartID,DepartName
表2([User]):UserID,UserName,DepartID
目的:选择部门后第二个下拉框得到该部门下的用户名
首先利用利用STRUTS的ACTION在进入页面时就为“部门”下拉框初始化值,选择“部门”后利用JS的onchange事件联动更新“用户”下拉框,以下是代码:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html"%>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@ page isELIgnored="false"%>
<html:html>
   <HEAD> 
   <META http-equiv="Content-Type" content="text/html; charset=gb2312"> 
   <title>发起新的年度任务</title>
   <LINK rel=stylesheet type=text/css href=style.css>
  
   <% 
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 
   String url ="jdbc:sqlserver://localhost:1433; DatabaseName=test" ; 
   String username="sa";
   String password="123";
   Connection conn= DriverManager.getConnection(url,username,password); 
   Statement stmt=conn.createStatement(); 
   ResultSet rs; 
   %> 
  <script language = "JavaScript"> 
  var onecount; 
  onecount=0; 
  subcat = new Array(); 
  <% 
        int count = 0; 
rs=stmt.executeQuery("select * from [User]"); 
while(rs.next()) 

String userName=new String(rs.getString("UserName").getBytes("GB2312"),"ISO-8859-1"); 
%> 
subcat[<%=count%>] = new Array("<%="userName"%>","<%=rs.getInt("UserID")%>","<%=rs.getInt("DepartID")%>");  <% 
count = count + 1; 

%> 
onecount=<%=count%>;  function changelocation1(id) 

document.forms[0].user1.length = 0;  var id=id; 
var i; 
for (i=0;i < onecount; i++) 

if (subcat[i][2] == id) 

document.forms[0].user1.options[document.forms[0].user1.length] = new Option(subcat[i][0], subcat[i][1]); 

}  }  function changelocation2(id) 

document.forms[0].user2.length = 0;  var id=id; 
var i; 
for (i=0;i < onecount; i++) 

if (subcat[i][2] == id) 

document.forms[0].user2.options[document.forms[0].user2.length] = new Option(subcat[i][0], subcat[i][1]); 

}  } function changelocation3(id) 

document.forms[0].user3.length = 0;  var id=id; 
var i; 
for (i=0;i < onecount; i++) 

if (subcat[i][2] == id) 

document.forms[0].user3.options[document.forms[0].user3.length] = new Option(subcat[i][0], subcat[i][1]); 

}  } </script> 
 </HEAD>
  <body bgcolor="#DCDADA">
  当前位置:信息中心-->发起新的年度任务<br>
  <table border="1" CELLSPACING=0 CELLPADDING=0>
   <html:form action="/addAnnual.do" method="post">
   <tr><td align="center" colspan="3">
   发起新的年度任务
   </td></tr>
   <tr><td align="center" colspan="3">
   <font color="red"><html:errors/></font>
   </td></tr>
   <tr><td align="right">任务名称:</td>
   <td colspan="2" align="left">
  <html:text size="40" property="taskName"/>   </td></tr>
   <tr><td align="right">任务详情:</td>
   <td colspan="2" align="left">
  <html:textarea property="taskDetail" rows="10" cols="40"/>   </td></tr>
   <tr>
     <td rowspan="3" align="right">发送给:</td>
     <td height="28" align="left">部门:
        <html:select property="department1" onchange="changelocation1(document.forms[0].department1.options[document.forms[0].department1.selectedIndex].value)">
          <html:option value="0">--请选择部门--</html:option>
          <c:forEach items="${requestScope.rsArray1}" var="record1">
       <html:option value="${record1[0]}">${record1[1]}</html:option>
     </c:forEach>
        </html:select></td>
     <td align="left">人员:
        <html:select property="user1">
          <html:option value="0">--请选择用户--</html:option>
        </html:select></td>
  <tr>
    <td height="27" align="left">部门:
          <html:select property="department2" onchange="changelocation2(document.forms[0].department2.options[document.forms[0].department2.selectedIndex].value)">
            <html:option value="0">--请选择部门--</html:option>
            <c:forEach items="${requestScope.rsArray1}" var="record1">
        <html:option value="${record1[0]}">${record1[1]}</html:option>
       </c:forEach> 
        </html:select></td>
    <td align="left">人员:
          <html:select property="user2">
            <html:option value="0">--请选择用户--</html:option>
        </html:select></td>
      <tr>
       <td height="27" align="left">部门:
          <html:select property="department3" onchange="changelocation3(document.forms[0].department3.options[document.forms[0].department3.selectedIndex].value)">
            <html:option value="0">--请选择部门--</html:option>
            <c:forEach items="${requestScope.rsArray1}" var="record1">
         <html:option value="${record1[0]}">${record1[1]}</html:option>
       </c:forEach> 
            </html:select></td>
    <td align="left">人员:
          <html:select property="user3">
            <html:option value="0">--请选择用户--</html:option>
        </html:select></td>   
  <tr>
    <td align="center" colspan="3">&nbsp;</td>
      </tr>
  <tr><td align="center" colspan="3">
  <input type="submit" value="提 交">
   </td></tr>
   </html:form>      
   </table>
 </body>  
</html:html>现在出现的问题是选择“部门”后,“用户”下拉框出现的不是用户名而是一个字符串“userName”
因此我怀疑是以上代码中加粗部分出错,但不知道怎么改,请大家帮帮我。

解决方案 »

  1.   

    subcat[ <%=count%>] = new Array(" <%="userName"%>"," <%=rs.getInt("UserID")%>"," <%=rs.getInt("DepartID")%>"); 
    试试这个吧subcat[<%=count%>] = new Array(<%=userName%>,<%=rs.getInt("userID"),<%=rs.getInt("DepartId")%>)
    不知道对不对,.
      

  2.   


    var userName = "<%=userName%>";
    var userId = "<%=rs.getInt("userId")>";
    var departId = "<%=rs.getInt("DepartId")%>";
     
    subcat[<%=count%>] = new Array(userName,userId,departId);