-2

我在用 JSP 设计的网页上有 multiSelect 下拉菜单。最终用户将选择所需的选项并将表单保存到数据库。下面是从数据库主表中获取值和索引的原始下拉列表。选定的值保存在另一个表中。

页面上的原始下拉菜单。

 <table>
 <tr>
 <td> Departments Impacted   </td>
    <td><select multiple="multiple" name="abc" id="abc">
     <%

         Statement stt3 = conn.createStatement();
         ResultSet rstt3 = stt3.executeQuery("SELECT DEPT_DEPARTMENT_ID, DEPT_NAME FROM [PA].[dbo].[PA_DEPT_DEPARTMENT] ORDER BY DEPT_NAME ASC");
         while (rstt3.next()) {

         %>

         <option value="<%=rstt3.getString(1)%>"><%=rstt3.getString(2)%>
         </option>
         <%
             }
            stt3.close();
     rstt3.close();
                            %>
    </select></td>  </tr>

下面的代码检索保存的数据库值并将它们显示为在新的选择标记中选择。但是,我希望所选值显示在上表中,而不是新的下拉列表中。

<%
 ArrayList al = new ArrayList();    
 String dept = null;
 String deptID = "SELECT DEPARTMENT_ID FROM  [PA_RD_DEP_REQ_DETAILS_DEPARTMENT_MAP] inner join  [PA_RD_REQUISITION_DETAILS] on RD_DEP_RD_ID=RD_ID where RD_ID=?";

         PreparedStatement pst4 = conn.prepareStatement(deptID);
         pst4.setString(1, RD_ID);
         ResultSet rstt1 = pst4.executeQuery();
         while (rstt1.next()) {
             dept=rstt1.getString(1);
            al.add(dept);

         }
         for(int i=0; i<al.size(); i++)
         {
         String deptName = "SELECT [DEPT_NAME] FROM  [PA_DEPT_DEPARTMENT] WHERE [DEPT_DEPARTMENT_ID]=?";
         PreparedStatement pst5 = conn.prepareStatement(deptName);
         pst5.setString(1, (String)al.get(i));
         ResultSet rstt5 = pst5.executeQuery();
         while (rstt5.next()) {
             %>
             <option value="<%=(String)al.get(i)%>" selected="selected"><%=rstt5.getString(1)%>
             <%  


         }
         }


         %>
4

1 回答 1

0

selected 属性, 添加到option标签将使其被选中。这可以为多个选项标签完成,而不需要 JavaScript:

<select multiple="multiple">
  <option selected="selected" value="volvo">Volvo</option>
  <option selected="selected" value="saab">Saab</option>
  <option value="vw">VW</option>
  <option value="audi">Audi</option>
</select>
于 2012-11-06T21:00:09.450 回答