java 如何使用复选框将选定行中的数据从 JSP 传递到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4525230/
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
How to pass data from selected rows using checkboxes from JSP to the server
提问by eddy
I'd like to know if there's any way to send data to the server for the selected rows using the checkboxes I've put on those rows? I mean , how can I send only the data (in this case mileage) of those selected rows (selected with the checkboxes) to the server ? see the image
我想知道是否有任何方法可以使用我放在这些行上的复选框将所选行的数据发送到服务器?我的意思是,如何仅将那些选定行(用复选框选中)的数据(在本例中为里程)发送到服务器?看图片
Here's the html code I use:
这是我使用的html代码:
<table>
<thead>
<tr class="tableheader">
<td width="10%"></td>
<td width="30%">Vehicle</td>
<td width="40%">Driver</td>
<td width="10%">Mileage</td>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="item">
<tr>
<td align="center">
<input type="checkbox" name="selectedItems"
value="c:out value="${item.numberPlate}"/>"/>
</td>
<td align="left"><c:out value="${item.numberPlate}"/></td>
<td align="left"><c:out value="${item.driver.fullName}" /></td>
<td align="left"><input type="text" name="mileage" value="" /></td>
</tr>
</c:forEach>
</tbody>
</table>
I really hope you can give some guidance on this.
我真的希望你能在这方面给予一些指导。
Thanks in beforehand.
提前致谢。
回答by BalusC
Change the mileage
input as follows:
更改mileage
输入如下:
<c:forEach items="${list}" var="item">
<tr>
<td align="center">
<input type="checkbox" name="selectedItems"
value="<c:out value="${item.numberPlate}"/>"/>
</td>
<td align="left"><c:out value="${item.numberPlate}"/></td>
<td align="left"><c:out value="${item.driver.fullName}" /></td>
<td align="left"><input type="text" name="mileage_<c:out value="${item.numberPlate}"/>" value="" /></td>
</tr>
</c:forEach>
Gather it as follows:
收集它如下:
String[] selectedItems = request.getParameterValues("selectedItems");
for (String selectedItem : selectedItems) {
String mileage = request.getParameter("mileage_" + selectedItem);
// ...
}
No need for nasty JS workarounds.
不需要讨厌的 JS 解决方法。
回答by Dewfy
According to HTML's FORM spec, items with same name will be grouped to single value separated by comma. So just bind your bean to form result and split value of 'selectedItems' by comma.
根据 HTML 的 FORM 规范,具有相同名称的项目将被分组为以逗号分隔的单个值。因此,只需将您的 bean 绑定到表单结果并用逗号分割 'selectedItems' 的值。
回答by RoToRa
First off, I'd suggest to make sure that your server-side script ignores the "non-checked" data, because you never can be sure that the JavaScript will actually work or isn't manipulated by the user.
首先,我建议确保您的服务器端脚本忽略“未检查”数据,因为您永远无法确定 JavaScript 是否会实际工作或不会被用户操纵。
However currently you have no way too associate each check box with its mileage input, so you should consider giving each an individual name such as {numberplate}-mileage
.
但是,目前您无法将每个复选框与其里程输入相关联,因此您应该考虑为每个复选框提供一个单独的名称,例如{numberplate}-mileage
.
That would also simplify the JavaScript. (I'm using jQuery here, becuase it quicker):
这也将简化 JavaScript。(我在这里使用 jQuery,因为它更快):
jQuery("input[name=selectedItems]").click(
var chkbox = $(this);
$("input[name=" + checkbox.val() + "-mileage]").attr("disabled", checkbox[0].checked ? "" : "disabled");
);
(Disabled input elements don't submit their value).
(禁用的输入元素不提交它们的值)。