简体   繁体   English

为什么我的加载更多按钮不管我做什么都不起作用?

[英]Why my load more button won't work whatever I do?

I've been struggling with this the whole day, I just can't get it working for I don't know which reason.我一整天都在为此苦苦挣扎,我只是无法让它工作,因为我不知道是什么原因。 The doubt is because I have another section on the same page and it works perfectly.疑问是因为我在同一页面上还有另一个部分,它运行得很好。

I have a form, and when that form gets submitted I show results below without refresh with the help of AJAX.我有一个表单,当该表单被提交时,我会在 AJAX 的帮助下在不刷新的情况下显示以下结果。 Now at the bottom I have a Load More button which should calculate all outputted results and limit them to 3. So when the Load More button is clicked it will show 3 more results and so on.现在在底部我有一个Load More按钮,它应该计算所有输出的结果并将它们限制为 3。所以当点击Load More按钮时,它将显示 3 个更多的结果,依此类推。

My PHP + HTML:我的 PHP + HTML:

<?php 
$comments = mysqli_query($conn, "SELECT * FROM comments WHERE feed_id='$feed_token' ORDER by posted_at DESC");
?> 
<div class="storelisting<?php echo $post_inc2; ?>" id="storelisting<?php echo $post_inc2; ?>">
<?php
while($comments_row = mysqli_fetch_array($comments)) {             
  $comment = $comments_row['comment'];
  $comments_posted_at = $comments_row['posted_at'];
  $user_token = $comments_row['user_id'];

  $user_firstname = mysqli_query($conn, "SELECT firstname FROM users WHERE token='$user_token'");
  $user_username = mysqli_query($conn, "SELECT username FROM users WHERE token='$user_token'");
  $user_logo = mysqli_query($conn, "SELECT logo FROM users WHERE token='$user_token'");
  $comment_firstname = mysqli_fetch_row($user_firstname);
  $comment_username = mysqli_fetch_row($user_username);
  $comment_logo = mysqli_fetch_row($user_logo);
?>

<div class="comments">
  <div class="main">
    <figure class="user mb-0 mr-3"><img class="post-img-img comment-img" src="assets/img/user_images/<?php if ($comment_logo[0] == '') { echo 'user-top.png'; }  else { echo $comment_logo[0]; } ?>"></figure>
    <div>
      <h5 class="modal-title comments-modal-title" id="exampleModalLongTitle"><?php echo $comment_firstname[0]; ?><span class="bullet"> &sdot; </span><span class="comments-subtitles">@<?php echo $comment_username[0]; ?></span><span class="bullet"> &sdot; </span><span class="comments-subtitles"><?php echo timeago($comments_posted_at); ?></span></h5>
      <div class="modalUsername get-space">Replaying to <span class="reply-to">@<?php echo $username[0]; ?></span></div>
      <p class="commentMsg"><?php echo $comment; ?></p>
    </div>
  </div>
</div>

<?php } ?>

</div>

<div id="loadMoreComments<?php echo $post_inc2; ?>">Load More</div> 

jQuery (Updated): jQuery(更新):

   $('.storelisting<?php echo $increase; ?> .comments').css('display','none');
$(document).ready(function () {
    size_li = $(".storelisting<?php echo $increase; ?> .comments").length;
    x=3;
    $('.storelisting<?php echo $increase; ?> .comments:lt('+x+')').show();
    if (size_li <= x) {
    $('#loadMoreComments<?php echo $increase; ?>').remove();
    }
    $('#loadMoreComments<?php echo $increase; ?>').click(function () {
        x= (x+3 <= size_li) ? x+3 : size_li;

        setTimeout(function(){
        $('.storelisting<?php echo $increase; ?> .comments:lt('+x+')').fadeIn("slow");
      }, 1000);

        if(x == size_li){
        setTimeout(function(){
          $('#loadMoreComments<?php echo $increase; ?>').hide();
        }, 1000);
        }
    });
});

So the PHP code gets all the rows from the selected column/table, that works great, with the AJAX I show submitted comments bellow, which works great too.所以 PHP 代码从选定的列/表中获取所有行,效果很好,我在下面展示了 AJAX 提交的评论,这也很好。 But the Load more button doesn't do nothing when its clicked.但是加载更多按钮在单击时不会执行任何操作。 The script shows only three divs and hide others, but when clicked it doesn't respond.该脚本仅显示三个 div 并隐藏其他 div,但单击时它没有响应。 The $post_inc2 and the $increase variables increases the number on the divs (because I have multiple feeds that lets you comment on it). $post_inc2$increase变量增加了 div 上的数量(因为我有多个提要让您对其发表评论)。

Any help will mean a lot, since I'm really frustrated.任何帮助都意义重大,因为我真的很沮丧。

A few pointers:几点提示:

  1. Oftentimes .show() won't do anything unless you do .hide() first, and vise versa.通常.show()不会做任何事情,除非你先做.hide() ,反之亦然。 You can chain them, eg $(el).hide().show() ... it's probably this.您可以将它们链接起来,例如$(el).hide().show() ... 可能就是这个。
  2. Otherwise make sure your selectors are working by assigning your $('selector') calls to a variable and then doing a console.log(thatVar.length) to make sure each of them got the number of results you expected.否则,通过将您的$('selector')调用分配给一个变量,然后执行console.log(thatVar.length)以确保它们中的每一个都获得了您期望的结果数量,从而确保您的选择器正在工作。
  3. Finally it's possible you are attempting to attach your click listener before the element is ready in DOM.最后,您可能正在尝试在元素在 DOM 中准备好之前附加您的点击侦听器。 Best practice is always to attach listeners either inline or (in your case) during a DOM ready event:最佳做法始终是在 DOM 就绪事件期间内联或(在您的情况下)附加侦听器:
$(document).ready(function() {
  // all your JQuery code shown above
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM