【问题标题】:How to pass an array of checkbox value From one JSP page to another如何将一组复选框值从一个 JSP 页面传递到另一个
【发布时间】:2013-08-31 18:44:03
【问题描述】:

我是初学者。我想将一组复选框值从一个 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 

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

【问题讨论】:

  • hString[]。迭代它的元素。你看到的是Object#toString()

标签: java javascript mysql jsp


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以使用 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>')
    
     }
    

    【讨论】:

      【解决方案3】:
      for(int count=0; count<h.length; count++){
          // DO SOME OPERATION on h[count];
      }
      

      另外,只是一个建议,请不要将变量命名为qns[],您可以通过说selectedItems来保持简单

      【讨论】:

        【解决方案4】:

        你得到的是一个数组,所以你需要使用索引来获取元素,比如:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-04-17
          • 1970-01-01
          • 1970-01-01
          • 2015-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多