简体   繁体   English

使用Javascript将选中的复选框值传递给另一个JSP

[英]Passing checked checkboxes values to another JSP using Javascript

This is ,my code in JSP. 这是我在JSP中的代码。 I need to pass checked checkbox values from a.jsp to b.jsp through Javascript. 我需要通过JavaScript将选中的复选框值从a.jsp传递到b.jsp。 I have no idea how to do it. 我不知道该怎么做。 Please help me. 请帮我。

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <form method="post" action="b.jsp">
            My Favourite Colors are
            <input type="checkbox" name="cb1" value="blue">Blue
            <input type="checkbox" name="cb1" value="green">Green
            <input type="checkbox" name="cb1" value="Yellow">Yellow
            <input type="checkbox" name="cb1" value="Red">Red
            <input type="checkbox" name="cb1" value="white">White
            <input type="submit">
        </form>
    </body>
</html>

You can not directly post data to 'b.jsp'. 您不能直接将数据发布到“ b.jsp”。 You will need to create a servlet in middle. 您将需要在中间创建一个servlet。

<form method="post" action="ServletNameHere">

When the form is submitted, The control will go to the servlet. 提交表单后,控件将转到servlet。

<input type="submit">

In the servlet you can get the data you have sent from 'a.jsp' put it in a model and redirect to b.jsp. 在Servlet中,您可以获取从“ a.jsp”发送的数据,并将其放入模型中并重定向到b.jsp。

Fetch the values from the model. 从模型中获取值。

There is no need to use JavaScript or jQuery . 无需使用JavaScriptjQuery When we click on submit button then the checked values will be sent to the b.jsp and in b.jsp we can access the values from the request object. 当我们单击submit按钮时, checked值将被发送到b.jsp ,在b.jsp我们可以从request对象访问这些值。 To access the values use the following code in b.jsp : 要访问这些值,请在b.jsp使用以下代码:

<%
   String[] values = request.getParameterValues("cb1");

   if (values!=null)
     for (String val: values) {
        System.out.println("Value "+ val);
    }
%>

Note : If no checkbox checked then request.getParameterValues() method returns null value. 注意 :如果未选中任何复选框,则request.getParameterValues()方法将返回null值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM