微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何在下一个查询中使用jQuery AJAX .done变量

我想知道如何从done方法返回页面var,以便下次点击“.morePlease”时它会发送新的页面值(由我的后端PHP增加并返回并返回).

(function($) {
    var page ='';

$(document).on( 'click', '.morePlease', function( event ) {
    event.preventDefault();
    getMorePosts(page);
})

function getMorePosts(page) { 
    $.ajax({
    url: '<?=$ajaxUrl?>',
    type: 'post',
    data: {
    action: 'ajax_pagination',
    query_vars:'<?=$query?>',
    page: page
    }
    }).done(function(response) {
        var obj=$.parseJSON(response);
        page = obj.page;
    }).fail(function() {
       alert( 'no result ' ); 
    });
}
})(jQuery);

解决方法:

您可以将其保留在外部,以使其在您的所有功能中全局且可访问

  page = 1;
 // Now this page will be initialized once on page reload and its value 
 // will exists and changing on entire page
 (function($) {
      $(document).on( 'click', '.morePlease', function( event ) {
         event.preventDefault();
         getMorePosts(page);
      })

     function getMorePosts(page) { 
         $.ajax({
             url: '<?=$ajaxUrl?>',
             type: 'post',
             data: {
                 action: 'ajax_pagination',
                 query_vars:'<?=$query?>',
                 page: page
             }
         }).done(function(response) {
             var obj=$.parseJSON(response);
             page = obj.page;
         }).fail(function() {
             alert( 'no result ' ); 
         });
   }
})(jQuery);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐