【发布时间】:2018-10-17 22:12:34
【问题描述】:
我是 jsp 的初学者。我想从数据库中获取数据并根据 db 值(即 0 或 1)在复选框上显示选中状态。这对我有用。但我也希望当我选中或取消选中复选框它将重置值并将其作为 href 参数传递给控制器。 这是我的jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Investor Account</title>
</head>
<body>
<script type="text/JavaScript">
function updateCheck(){
if(document.getElementById('chk').checked){
document.getElementById('chk').value = 1;
location.href=""
alert(document.getElementById('chk').value);
}
else{
document.getElementById('chk').value = 0;
alert(document.getElementById('chk').value);
}
}
</script>
<div align="center">
<h1>Investor Account List</h1>
<table border="1">
<th>ID</th>
<th>Investor Code</th>
<th>Investor Name</th>
<th>SMS Subscriber</th>
<th>Action</th>
<c:forEach var="investorAcc" items="${listInvestorAcc}" varStatus="st">
<tr>
<td>${st.index + 1}</td>
<td>${investorAcc.investor_code}</td>
<td>${investorAcc.investor_name}</td>
<td> <input type="checkbox" id="chk" name="chkSms" value= <c:if test="${investorAcc.sms_sub==1}">1</c:if> onclick="updateCheck();"
<c:if test="${investorAcc.sms_sub==1}">checked="checked"</c:if>/>
<td>
<a href="updateInvestorAcc?id=${investorAcc.id}&chkSms=<c:if test="${chkSms==1}">1</c:if>"> Update </a>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
【问题讨论】:
-
ID 必须是唯一的,您多次使用
chk作为 id -
为什么要在href中作为参数传递?
-
因为我的控制器中有 /updateInvestorAcc 的映射,而且我不仅要传递复选框值,还要传递列表中的 id。我不知道其他方法可以做到这一点:-p跨度>