简体   繁体   English

将所有选中的复选框值从动态生成的JSP表中获取到servlet?

[英]Get all checked checkbox values to servlet from dynamically generated JSP table?

I'm displaying the results of my query from Table A in my results.jsp as goes: 我正在按以下方式在我的results.jsp中显示来自Table A查询结果:

<form action="InsertToTableB" method="post">
       <table>
            <tr>
                <th>Select</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>

            <c:forEach var="user" items="${resultData}">
            <tr>
                <td><input type="checkbox" name="selected" value="<c:out value='${user.userID }' />"/></td>
                <td><c:out value="${user.fName }"/></td>
                <td><c:out value="${user.lName }"/></td>
                <td><c:out value="${user.email }"/></td>    
            </tr>
            </c:forEach>    
        </table>
   <button type="submit" value="Submit">Insert to Table B</button>
   <button type="submit" value="Submit">Delete from Table A</button>
</form>

And this form is being passed into my InsertToTableB servlet. 并将此form传递到我的InsertToTableB servlet中。

How do I get ONLY the selected values from the generated table <input type="checkbox" name="selected" value="<c:out value='${user.userID }' />"/> and how do I know which button ( Insert to Table B or Delete from Table A ) has been pressed? 如何仅从生成的表中获得所选值<input type="checkbox" name="selected" value="<c:out value='${user.userID }' />"/>知道按下了哪个按钮(“ Insert to Table B或“ Delete from Table A )?

I've only worked with <input type="text"> and getting the value into the servlet using .getParameter('name') and those are Strings . 我只使用过<input type="text">并使用.getParameter('name')将值输入到servlet中,这些是Strings

Is it the same for checkbox input type? checkbox输入类型是否相同?

Can I have 2 or more submit buttons in a form each with a different function? 我可以在表单中有2个或更多具有不同功能的提交按钮吗?

A picture of my table with sample data: 我的表格图片和示例数据:

在此处输入图片说明

You need change name='selected' to checked='checked' 您需要将name='selected'更改为checked='checked'

so change 所以改变

<input type="checkbox" name="selected" value="<c:out value='${user.userID }' />"/>

to

<input type="checkbox" checked="checked" value="<c:out value='${user.userID }' />"/>

To get all the checked checkbox ,you can use jQuery like below: 要获得所有选中的checkbox ,可以使用如下所示的jQuery

var checkedEles = $("input:checkbox:checked");

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

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