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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18492203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:30:14  来源:igfitidea点击:

How to pass an array of checkbox value From one JSP page to another

javajavascriptmysqljsp

提问by Salini L

I am a beginner. I want to pass an array of check box values from one JSP page to another. The page getting data is

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

How can i receive check box values in JSP another page. I have tried the following code but its not working properly

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

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

But its passing the value

但它传递了价值

[Ljava.lang.String;@a0a595 

Please somebody help me to solve this problem.

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

采纳答案by Mazhar

You Can use it as follows. in Form

您可以按如下方式使用它。通知

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

In process.jsp

进程中.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
    }

回答by Juned Ahsan

You are getting an array so you need to get the elements using index, such as:

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

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

or use a loop to iterate entire array.

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

回答by JNL

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

Also, just a recommendation, please do not name the variables as qns[], you can always keep it simple by saying selectedItems

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

回答by AhmadDani

You may use stringbuilder(), i hope it's works:

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

 }