我正在尝试为我的评论实现一个 jquery/ajax 分页系统。我已经实现了 jquery 删除和插入评论功能。现在我在完全实现分页部分时遇到了麻烦。我的一些代码如下。基本上,在加载更多帖子的函数中,如果用户单击删除评论按钮或插入评论按钮,我希望变量计数重置为零。这些按钮的 ID 分别是“删除评论”和“添加评论”。本质上,count 变量被传递给一个查询,其中使用 LIMIT $page,5。如果可以,请提供帮助。谢谢。
$(document).ready(function() {
var count = 0;
var commentcount = "<?= $commentcount ?>";
if(commentcount <= 5){
$("#more").hide();
}
if(commentcount >= 6){
$("#more").show();
}
$("#more").click(loadPosts);
function loadPosts() {
var num = 5;
$(".loading").show("fast");
count += num;
$.post("pull.php?id=<? echo $id; ?>", {'page': count}, function(data){
$("#commentarea").append(data);
$(".loading").hide("slow");
var commentcount = "<?= $commentcount ?>";
if(commentcount <= (count + num) && count >=5){
$("#more").hide();
}
});
}
});
最佳答案
在 loadPosts 中你有:
var commentcount = "<?= $commentcount ?>";
PHP 代码在页面首次加载时运行,而不是在 AJAX 请求时运行。因此,当 loadPosts 函数运行时,commentcount 被设置为其当前值。
你可能想从你的 pull.php 返回 JSON 数据而不是......像这样:
$commentcount = /* however you get your count */;
$returnData = array( 'commentcount' => $commentcount, 'comments' => $comments );
echo json_encode( $returnData );
然后在你的 JS 中:
$('#commentarea').append( data.comments );
var commentcount = data.commentcount;
关于php - jquery更多按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9536154/