我有一个带功能报告的div,删除注释按钮和内部的报告模式,还包括jstl foreach从servlet获取数据以显示div中的注释列表。这是我的代码的一部分:
<div class="comment-list" id="comment-div">
<c:forEach items="${commentList}" var="items">
<div class="media-body">
<input type="hidden" id="commentUserId" name="commentUserId" value="${items.accountId.accountId}"/>
<h4 id="commentUsername" class="media-heading">${items.accountId.userName}
<small><i>Posted on ${items.time}</i></small>
</h4>
<p id="show-comment">${items.content}</p>
<a id="${items.commentId}" name="btnReportComment" class="btn btn-report">Report</a>
<div class="modal fade" id="reportModalComment" role="dialog">
<!-- report modal content -->
</div>
<c:if test="${sessionScope.user.userName==items.accountId.userName}"><a id="${items.commentId}" name="btnDeleteComment"
class="btn btn-delete" rel="2" style="margin-left: 20px">Delete</a></c:if>
</div>
</c:forEach>
我使用ajax发表评论,当我点击帖子按钮时,它成功调用ajax函数我希望它重新加载评论列表,其功能包括删除评论和报告评论工作。
我确实使用了$('#comment-div').load(location.href + " #comment-div");
它重新加载了div而只重新加载了文本和数据,当我点击报告按钮或删除按钮时没有发生任何事情。
任何人都可以帮助我
编辑: 这是我的删除按钮功能:
function deleteComment(){
$('a[name=btnDeleteComment]').click(function(){
var commentId = $(this).attr('id');
var currentRow = $(this).parent().parent();
if (userId == null || userId == '') {
swal("Request Failed!", "You need to login first!", "warning");
} else {
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this comment!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-primary",
confirmButtonText: "Yes, delete it",
cancelButtonText: "Cancel",
}).then( function (result){
if(result.value){
$.ajax({
type: "POST",
url: "UserDeleteComment",
data:{commentId: commentId},
success: function(resultData){
swal("Done!","Your comment has been deleted!", "success");
currentRow.remove()
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
})
}
})
}
})