【发布时间】:2014-07-23 16:31:55
【问题描述】:
我已经动态生成了表格,
<c:forEach var="user" items="${usermap}">
<tr>
<td>${user.getUserName()}</td>
<td>${user.getIsActive()}</td>
<td class="status">${user.getBadLoginAttempts()}</td>
<c:choose>
<c:when test="${user.getBadLoginAttempts()=='Active'}">
<td><a href="#" class="btn btn-sm btn-primary unlockBtn" id="togBtn_${user.getUserName()}" >Lock</a></td>
</c:when>
<c:otherwise>
<td><a href="#" class="btn btn-sm btn-primary unlockBtn" id="togBtn_${user.getUserName()}" >UnLock</a></td>
</c:otherwise>
</c:choose>
</tr>
</c:forEach>
我在点击链接时做一些数据库操作,它工作正常。所以脚本是,
$(document).ready(function(){
$(document).on('click','.unlockBtn',(function (e) {
//get the user id
var $this = $(this);
var userStatus=$(this).closest('tr').find('td:eq(2)').text();
if(userStatus=='Active'){
alert("Active in if");
alert(userStatus);
$.post('<%=request.getContextPath()%>/controller/UserLockController',{'userName':$(this).closest('tr').find('td:eq(0)').text()},
function(data)
{
$this.closest('tr').find('.status').html(data);
$this.closest('tr').find('td:eq(3)').replaceWith("<td><a href='#' class='btn btn-sm btn-primary unlockBtn' >Unlock</a></td>");
});
}
else{
alert(userStatus);
$.post('<%=request.getContextPath()%>/controller/UserUnlockController',{'userName':$(this).closest('tr').find('td:eq(0)').text()},
function(data)
{
$this.closest('tr').find('.status').html(data);
$this.closest('tr').find('td:eq(3)').replaceWith("<td><a href='#' class='btn btn-sm btn-primary unlockBtn' >Lock</a></td>");
});
}
}));
});
所以每个功能都很好。我面临的问题是每一行我只能锁定或解锁一次。我一直想知道如何实现它。请帮帮我。
【问题讨论】:
-
而不是使用 document.ready 尝试使用类似的东西 $(document).bind('DOMSubtreeModified', function () {}
-
请注意您使用的 jQuery 版本。弃用在这里:api.jquery.com/on