在javascript中动态更改表内容

时间:2014-06-09 07:17:58

标签: javascript html post

我有一个动态生成的表,

<c:forEach var="user" items="${usermap}">

<tr>
<td>${user.getUserName()}</td>
<td>${user.getIsActive()}</td>
<td id="status_${user.getUserName()}">${user.getBadLoginAttempts()}</td>
<c:choose>
<c:when test="${user.getBadLoginAttempts()=='Active'}">
        <td><a href="#" class="btn btn-sm btn-primary unlockBtn"  onClick="LockAccount('${user.getUserName()}','${user.getBadLoginAttempts()}')" >Lock</a></td>
</c:when>
<c:otherwise>
    <td><a href="#" class="btn btn-sm btn-primary unlockBtn" onClick="LockAccount('${user.getUserName()}','${user.getBadLoginAttempts()}')">UnLock</a></td>
</c:otherwise>
</c:choose>
</tr>
/c:forEach>

表生成正常,点击链接&#34;锁定&#34;第3列值应该更改已锁定且链接必须更改&#34;解锁&#34;反之,所以在javascript中我在Post方法中调用控制器,我面临的问题是,控制进入控制器并进行数据库更改并将文本写入PrintWriter obj现在我想把它放在相应的表格单元格中。怎么做请帮帮我。

我的Javascript是,

<script>

function LockAccount(userName,status){
alert(userName);
/* alert(status); */
   $.post('<%=request.getContextPath()%>/controller/UserLockController',{'userName':userName,'status':status},
function(data)
{
  document.getElementById("status_"+userName).value=html(data);
}); 
</script>

控制器发布功能是这样的,

protected final void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException     {


    String inputParam = request.getParameter("userName");
    System.out.println(inputParam);
    String status=request.getParameter("status");
    System.out.println(status);
    System.out.println();
    PrintWriter out = response.getWriter();
    ServiceManager.getHelperService(
            TeacherManagementHelper.class, request, response).lockUser(inputParam);
    out.println("Locked");


}

所以最后我想给出这样的效果

Fiddel

1 个答案:

答案 0 :(得分:0)

尝试替换

document.getElementById("status_"+userName).value=html(data);

document.getElementById("status_"+userName).innerHTML(data);