当我删除评论时,rowCount 不会更新为不包含实际评论数。因为我使用淡出功能而不是 remove() 功能,因为我希望它从视图中淡出。
我怎样才能先淡出然后删除?
已经尝试过了,但这并没有帮助:
$('#item_'+DbNumberID).fadeOut("slow", function() {
$(this).remove();
});
编码:
jQuery.ajax({
type: "POST",
url: "delete-comment.php",
dataType:"text",
data:myData,
success: function(response){
$('#item_'+DbNumberID).fadeOut("slow");
var rowCount = $('#comment li').length;
alert(rowCount);
return false;
},
error: function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
编辑:
这是我的完整代码。可以是简单模式吗?
//##### Send delete Ajax request to response.php #########
$("body").on("click", "#comment .del_button", function(e) {
e.preventDefault();
//e.returnValue = false;
var clickedID = this.id.split('-'); //Split string (Split works as PHP explode)
var DbNumberID = clickedID[1]; //and get number from array
var myData = 'action=delete&id='+ DbNumberID; //build a post data structure
jQuery.ajax({
type: "POST", // HTTP method POST or GET
url: "add-comment.php", //Where to make Ajax calls
dataType: "text", // Data type, HTML, json etc.
data: myData, //Form variables
success: function(response){
//on success, hide element user wants to delete.
$('#item_'+DbNumberID).fadeOut(500, function() {
setTimeout(function() {
$('#item_'+DbNumberID).remove();
}, 500);
});
var rowCount = $('#comment li').length;
rowCount--;
alert(rowCount);
if (rowCount == 0) {
$("#comment").html('<div class="PageWarning">No comments.</div></div>');
}
return false;
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
});