如果html中checkbox选中多条记录向后台传值,js实现如下
var orderIds=[];
var userCodes=[];
$("input[name='cBox']:checked").each(function(){
//选中的checkbox记录,循环取出每条记录中orderId和userCode
var orderId=$(this).val();
var userCode=$(this).attr("userCode");
orderIds.push(orderId);
userCodes.push(userCode);
});
$.ajax({
url:"batch.do",
data:"orderIds="+orderIds.join(",")+"&userCodes="+userCodes.join(","),
type:"post",
dataType:"text",
success: function(result){
alert(result);
}
});
}
java后台接收数据
@RequestMapping(value="batch.do")
public String batch(HttpServletResponse response,HttpServletRequest request,HttpSession session){
CustomerDBContextHolder.setDataSourceType(DataSourceInstances.WRITE_DATASOURCE);
String orderIds = request.getParameter("orderIds");
String[] orderId = orderIds.split(",");
String userCodes = request.getParameter("userCodes");
String[] userCode = userCodes.split(",");
html页面
<c:forEach items="${personList}" var="person" varStatus="n">
<tr class="<c:choose><c:when test="${n.index % 2==0 }">row-even</c:when><c:otherwise>row-odd</c:otherwise></c:choose>">
<td>
<input type="checkBox" name="cBox" userCode="${person.userCode"}/>
</td>
<td>
<input id="orderId" name="orderId" value="${person.orderId}" type="hidden"/>
</td>
</c:forEach>