dougu1985 2018-10-12 14:29
浏览 113
已采纳

加载更多按钮只能工作一次

I have a table named tbl_video that consist of two fields (video_id, video_title What I want to add a load more button using Ajax JQuery like social networking sites .I am able to get the first two records but when I try second time nothing happens. I dont get any errors .What am i doing wrong .

this my index.php file I create table structure here and then fetch two records.And get the second record's id value and send it to the more.php file

  <body>  

                    <table class="table table-bordered" id="load_data_table"> 

                    <thead>
                    <tr>
                    <td width="15%">Video Id</td>
                    <td>Video Title</td>
                    </tr>
                    </thead>
                    <?php

                    while ($row = mysqli_fetch_array($result)) {
                        $video_id = $row['video_id']; ?>
                             <tr>
                             <td><?php echo $row['video_id']; ?></td>
                             <td><?php echo $row['video_title']; ?></td>

                             </tr>
                        <?php
                    }

                    ?> 
                    <tr id="remove_row"><td><button type="button" id="more" class="btn btn-success form-control"
                     data-id="<?php echo $video_id; ?>">More</button></td></tr>

                    </table>  
               </div>  
          </div>  
     </body>  
</html>  
 <script>  
 $(document).ready(function(){  
     $('#more').click(function(){
 var id=$(this).data("id");


        $.ajax({
            url:"more.php",
            data:{id:id},
            method:"post",
            success:function(data){

              if(data!=''){

               $('#load_data_table').append(data);
               $('#remove_row').remove();


              }
              else{
                $('#more').html("No Data");
              }
            }

and this the more.php file.I think it is self explanatory

$id = $_POST['id'];
$output = '';
$db = mysqli_connect('localhost', 'root', '', 'testing');
$data = mysqli_query($db, "select * from tbl_video where video_id >{$id} limit 2");
if (mysqli_num_rows($data)) {
    while ($row = mysqli_fetch_array($data)) {
        $output .= '

         <tbody>
         <tr>
          <td>'.$row['video_id'].'</td>
          <td>'.$row['video_title'].'</td>
         </tr>

         ';
    }
    $output .= '
  <tr id="remove_row"><td><button type="button" id="more" class="btn btn-success form-control"
  data-id="'.$row['video_id'].'">More</button></td></tr></tbody>

  ';
    echo $output;
}

any help would be appriciated.

  • 写回答

1条回答 默认 最新

  • dongnue2071 2018-10-12 15:17
    关注

    When you add a new row and there is a new button for More

    It has the same id as the original button so there is a conflict there

    also the javascript needs to be initiliazed something like this

    PHP:

       $output .= '<tr id="remove_row"><td><button type="button"  class="more btn btn-success form-control" data-id="'.$row['video_id'].'">More</button></td></tr></tbody>';
    

    Javascript:

    <script>  
    function moreButton() {
    $('.more').click(function(){
    var id=$(this).data("id");
    
    
    $.ajax({
        url:"more.php",
        data:{id:id},
        method:"post",
        success:function(data){
    
          if(data!=''){
    
           $('#load_data_table').append(data);
           $('#remove_row').remove();
    
             moreButton();
          }
          else{
            $('.more:last').html("No Data");
          }
        }
     });
     }
     $(document).ready(function(){  
    
       moreButton();
     });
    

    Don't forget to remove the id of the more button to a class with .more

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启