【问题标题】:Passing dynamic values through ajax通过ajax传递动态值
【发布时间】:2012-12-26 19:11:50
【问题描述】:

我在 jquery 中有这段代码,它的编写方式是,当我向下滚动鼠标时,会从 db 中获取下一组记录并显示出来。最初我的页面显示 25 条记录,当我向下滚动下一个 15 条记录时,r 从 db 中获取。我的问题是,每当调用滚动功能时,我都无法设置计数器并增加计数器。而且每当我向下滚动时,都会显示相同的 15 条记录。 这是我的代码...

$(window).on('scroll',function(){
  if($(window).scrollTop()==($(document).height()-$(window).height())){

    $.ajax({
        type: 'POST',
        url: 'getdata.php',
        success: function(nxt_data){
           $('#data').append(nxt_data);
        }
    });
  } 
}); 

【问题讨论】:

    标签: php jquery mysql ajax


    【解决方案1】:

    创建一个页面变量,然后在每次调用 ajax 时添加到它。

    var page = 1;
    $(window).on('scroll', function () {
      if ($(window).scrollTop() == ($(document).height() - $(window).height())) {
        page++;
    
        $.ajax({
          type: 'POST',
          data: 'page=' + page,
          url: 'getdata.php',
          success: function (nxt_data) {
            $('#data').append(nxt_data);
          }
        });
      }
    });
    

    【讨论】:

      【解决方案2】:

      您的问题似乎是因为 getdata.php 无法知道要返回哪些记录,所以它只是返回相同的 15 行。

      var counter=25;
      
      $(window).on('scroll',function(){
        if($(window).scrollTop()==($(document).height()-$(window).height())){
      
          $.ajax({
              type: 'GET',
              url: 'getdata.php?start_row=' + counter,
              success: function(nxt_data){
                 $('#data').append(nxt_data);
                 counter += 15;
              }
          });
        } 
      }); 
      

      在您的 PHP 文件中,您可以使用 $_GET['start_row']; 访问计数器变量

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 2014-04-07
        • 2012-01-22
        • 1970-01-01
        • 2012-11-23
        • 2021-12-01
        • 2012-12-19
        相关资源
        最近更新 更多