JSP中的动态列表框

Dynamic listbox in JSP

本文关键字:列表 动态 JSP      更新时间:2023-09-26

我的JSP页面上有两个列表框。默认情况下,第一个列表框中填充有数据库数据。当用户在第一个列表中选择一个项目时,第二个列表框将使用Ajax相应地填充数据库数据。我是JSP的新手。我需要你的帮助。

我使用下面的JavaScript代码在第一个列表框中检索选定的值。

<script type="text/javascript" > 
  $(document).ready(function(){ 
    $("#rt_select").click(function() { 
      var option = $('#lstsprintid').val(); 
      alert(option); 
      return option; 
    }); 
  }); 
</script>

我无法在JSP页面中使用JavaScript返回的值。下面是我的多选列表框,数据来自数据库。

<p>Select Name :
<select size="3"  id="lstsprintid" multiple="multiple">
<%
while(rs.next())
{
 String name = rs.getString("s_name"); 
 %>
<option value="<%=name %>"><%=name %></option>
<%
}
%>
</select>           

我应该使用哪个JavaScript代码来获得在上面列表框中选择的值的列表。

 <%
 String s1 = request.getParameter("txt_test");
 out.println(s1);
 Statement st1= con.createStatement();
ResultSet rs1=st1.executeQuery("Select sprint_id from sprint where              sprint_name in ("+ s1 +")");
 %>

onchange事件与第一个列表和.selectedIndex一起使用以获得其选定值:

<script type="text/javascript" > 
   $("#lstsprintid").onchange(function() { 
      var option = $('#lstsprintid').selectedIndex; 
      alert(option); 
      return option;
      // and the use this value to fill the second list
   });  
</script>