dsjq6977 2013-06-12 05:40
浏览 115

AJAX“加载更多”按钮问题:点击后消失

EDIT: thanks everyone for your help. It still doesn't work though. This script works fine if i change the table to an ordered list. (tr = ol and td = li) That's why i really think the issue comes from the append part of the code. Any ideas?

Everything seems to work fine except for the "load more" button that disappears after it's been clicked. The new posts load but the users cannot click on the button again to load more posts. I've looked on SO but couldn't find a fix for my issue.

If I remove this line:

$("#more"+ID).remove();

then the button does show up but the gif image keeps on loading forever and the button is not clickable...

Here's the index.php code:

<table id="update_list" >
<tbody>
<?php
$result=query("SELECT * FROM postlist ORDER BY postid DESC LIMIT 9");
foreach ($result as $row)
{
$postid=$row['postid'];
$post=$row['post'];
?>
<tr id="<?php echo $postid ?>">
<td> 
<?php echo $post; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div id="more<?php echo $postid; ?>" class="morebox">
<a href="#" class="more" id="<?php echo $postid; ?>">more</a>
</div>
</div>
</body>
<script type="text/javascript">
$(function() {
$('.more').live("click",function() 
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="/loading.gif" />');

$.ajax({
type: "POST",
url: "loadmore.php",
data: "lastpost="+ ID, 
cache: false,
success: function(html){
$("#update_list >tbody").append(html);
$("#more"+ID).remove();
}
});
}
else
{
$(".morebox").html('The End');
}
return false;
});
});
</script>
</html>

Here is the loadmore php page:

<?php
include("includes/config.php");
if(isset($_POST['lastpost']))
{
$lastpost=$_POST['lastpost'];

$result=query("SELECT * from postlist WHERE postid < '$lastpost' ORDER BY postid DESC 
LIMIT 5"); 


foreach ($result as $row){
$postid=$row['postid'];
$post=$row['post'];
?>
<tr id="<?php echo update$postid ?>">
<td>
<?php echo $post; ?>
</td>
</tr>
<?php } ?>
<div id="more<?php echo $postid; ?>" class="morebox">
<a href="#" class="more" id="<?php echo $postid; ?>">more</a>
</div>   
<?php  }?>

Any tips on what i'm doing wrong here ? Thanks !

  • 写回答

2条回答 默认 最新

  • dongzu3511 2013-06-12 05:51
    关注

    You are removing the load more button after button clicked

    $("#update_list >tbody").append(html);
    //$("#more"+ID).remove(); remove this line
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?