2016-11-23 33 views
0

我學習AJAX和jQuery現在,我目前正在開發一個頁面,讓用戶可以喜歡/不喜歡後,和所有的職位將被加載的顯示與無限滾動,默認每次會加載5個帖子,但問題是如果帖子被無限滾動加載,那麼ajax按鈕將停止工作。我做了一些研究後,我知道這是因爲該函數沒有加載。但我找不到解決方案,有人可以幫我嗎?阿賈克斯停止,如果在laravel加載由無限滾動工作5.2

這是我的AJAX按鈕功能

$('.agree').on('click',function(event){ 
{ 
    event.preventDefault(); 
    postId = event.target.parentNode.parentNode.dataset['postid']; 
    var isAgree=event.target.previousElementSibling== null; 
    $.ajax({ 
     method:'POST', 
     url:urlAgree, 
     data:{ isAgree: isAgree,postId:postId,_token:token} 
    }) 
     .done(function() { 
      event.target.innerText = isAgree ? event.target.innerText == 'Agree' ? 'You agree this post' : 'Agree' : event.target.innerText == 'Disagree' ? 'You disagree this post' : 'Disagree'; 
      if (isAgree) { 
       event.target.nextElementSibling.innerText = 'Disagree'; 
      } else { 
       event.target.previousElementSibling.innerText = 'Agree'; 
      } 
     }); 

} 

});

這裏是無限滾動功能。

$(document).ready(function() { 
$(window).scroll(fetchPosts); 

function fetchPosts() { 

    var page = $('.endless-pagination').data('next-page'); 

    if(page !== null && page !== "") { 

     clearTimeout($.data(this, "scrollCheck")); 

     $.data(this, "scrollCheck", setTimeout(function() { 
      var scroll_position_for_posts_load = $(window).height() + $(window).scrollTop() + 100; 

      if(scroll_position_for_posts_load >= $(document).height()) { 
       $.get(page, function(data){ 
        $('.posts').append(data.posts); 
        $('.endless-pagination').data('next-page', data.next_page); 
       }) 

      } 
     }, 350)) 

    } 
} 

});

是否有這兩個功能結合在一起的任何可能的方式是什麼?

回答

0

委託點擊事件

$('.posts').on('click','.agree',function(event){ 
+1

謝謝它的工作!!!!!!!!雖然這很容易做到,但再次感謝你的貢獻。 – masterhunter