如何传递一个复选框值数组从一个JSP页面到另一个JSP页面

时间:2013-08-28 15:29:50

标签: java javascript mysql jsp

我是初学者。我想将一个JSP页面的复选框值数组传递给另一个JSP页面。获取数据的页面是

<%
     ResultSet rs=s.notapprovedqns();
 %>
 <%                
     while(rs.next())
     { %>
      <tr><td><input name="qns[]" type="checkbox" value="<% out.println(rs.getInt("question_id")); %>" /></td><td><center><%=rs.getString("question_id") %></center></td><td><%=rs.getString("question") %></td></td></tr>
     <% 
        }
      %>

我如何在JSP另一页中接收复选框值。我尝试了以下代码,但它无法正常工作

String[] h=null;
h=request.getParameterValues("qns[]");

但它传递了值

[Ljava.lang.String;@a0a595 

请有人帮我解决这个问题。

4 个答案:

答案 0 :(得分:6)

您可以按如下方式使用它。 在表格中

<form method="post" action="process.jsp">
    <input type="checkbox" name="list" value="value1">
    <input type="checkbox" name="list" value="value2">
    <input type="checkbox" name="list" value="value3">
</form>

在process.jsp

    String[] ids=request.getParameterValues("list");
    // this will get array of values of all checked checkboxes
    for(String id:ids){
     // do something with id, this is checkbox value
    }

答案 1 :(得分:1)

for(int count=0; count<h.length; count++){
    // DO SOME OPERATION on h[count];
}

另外,只是建议,请不要将变量命名为qns[],您可以通过说selectedItems

来保持简单

答案 2 :(得分:0)

您正在获取一个数组,因此您需要使用索引来获取元素,例如:

h=request.getParameterValues("qns[]");
String item = h[0]

或使用循环迭代整个数组。

答案 3 :(得分:0)

你可以使用stringbuilder(),我希望它有效:

 ResultSet rs=s.notapprovedqns();
 StringBuilder lstquestion = new StringBuilder();
 while(rs.next()) {
    String question_id = rs.getString("question_id");
    String question = rs.getString("question");
    lstquestion.append('<tr><td><input name="qns[]" type="checkbox" value='+question_id+' /></td><td><center>'+question_id+'</center></td><td>'+question+'</td></td></tr>')

 }
相关问题