简体   繁体   中英

Passing checked checkboxes values to another JSP using Javascript

This is ,my code in JSP. I need to pass checked checkbox values from a.jsp to b.jsp through Javascript. 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'. You will need to create a servlet in middle.

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

When the form is submitted, The control will go to the 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.

Fetch the values from the model.

There is no need to use JavaScript or jQuery . 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. To access the values use the following code in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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