weixin_33743880 2015-03-30 11:00 采纳率: 0%
浏览 41

JSP中的动态列表框

I have two listboxes on my JSP page. By default the first listbox is filled with database data, When the user selects an item in the 1st listbox, the second will filled accordingly with database data using Ajax. I am new to JSP. I need your help.

I used the below JavaScript code to retrieve the selected value in first listbox.

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

I am unable to use the value returned from JavaScript in my JSP page. Below is my multiple select listbox where data is coming from db.

<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>           

Which JavaScript code shall I use to get the list of values selected in the above listbox.

 <%
 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 +")");
 %>
  • 写回答

1条回答 默认 最新

  • weixin_33716154 2015-03-31 08:56
    关注

    Use the onchange event with the first list and .selectedIndex to get its selected value:

    <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>
    
    评论

报告相同问题?