2012-06-26 23 views
0

我從來沒有寫過Ajax,我目前正在嘗試編寫無限滾動頁面。當用戶向下滾動到頁面底部時,應該加載更多的項目,但現在它們沒有加載。這是我的Javascript功能來檢測時,他們見底並進行Ajax調用:JQuery後處理方法不執行

window.onload=function(){ 
//Find out how many items I have loaded and what filter I am using so I can make the Ajax call 
var vloaded = <?php echo $i; ?>; 
var vfilter = "<?php echo $filter ?>"; 
$(window).on('scroll', function() { 
    if ($(window).height() + $(window).scrollTop() >= $(document).height() - 10) { 
    //I have reached the bottom of the page, now load items 
    alert("loaded is " + vloaded + " and filter is " + vfilter); 
    $.post("/organizer/getMore", 
     { filter: vfilter, loaded: vloaded }, 
       function(responseText) { 
        $("grid").append(responseText); 
       },"html"); 
    //I've loaded the next 30 items, increment my counter for next time 
    vloaded +=30; 
    } 
}); 
} 

警報被顯示時,我打了底,我的變量是正確遞增。我使用Zend框架,所以該URL指向我getMoreAction()功能在這裏:

public function getmoreAction() 
{ 
//Used by Ajax to get more items for the infinite scroll 
    //Figure out how I'm filtering items and how many I've already loaded 
    $filter = $_POST['filter']; 
    $loaded = $_POST['loaded']; 
    echo "Filter is ".$filter; 
    echo "Loaded ".$loaded; 
    //Get all the items in the database ordered by filter 
    require_once(APPLICATION_PATH . '/models/ItemArray.php'); 
    $items = ItemArray::getItems($user->getID(), $filter, $loaded); 
    //Return items back to Ajax call, converted to html 
    echoItems($items); 
} 

我已經知道getItems功能的作品,因爲我還使用它時,第一次加載頁面,並echoItems只是一個循環來回顯每個項目的HTML,這也適用於其他地方。行動中的迴應永遠不會執行,所以我假設我的電話會議出現問題,以致我甚至從未接受過此操作。

+1

你使用螢火蟲嗎?如果沒有,那麼。它是Firefox的附加組件,可讓您快速查看ajax請求的發佈/響應(以及更多),以便您快速輕鬆地查看發生的情況。 –

+0

另外,如果您使用Chrome或IE,F12按鈕將顯示內置控制檯。 – efesar

+0

那麼,從螢火蟲我發現我得到一個致命的錯誤,從用戶沒有被定義。我解決了這個問題,但仍然沒有發生。每次向下滾動警報框時都會彈出,但不會加載新項目。 '$(「grid」)。append(responseText);'應該將變量'responseText'中包含的任何html附加到id爲'grid'的元素的末尾,對嗎?另外,在Firefox中,當我向下滾動10個像素時,彈出警告框,而不是當我向下滾動到底部的10個像素內時彈出。 – jaimerump

回答

1

2條建議。

  1. 使用jQuery功能$(document).ready()而不是window.onload屬性。
  2. 我重構,這樣我可以更容易地讀取它使用jQuery函數$.ajax()代替$.post()

// put these in the global scope 
var vloaded = <?php echo $i; ?>; 
var vfilter = "<?php echo $filter ?>"; 

$(document).ready() 
{ 

    // I forgot to leave this in 
    $(window).on('scroll', function() 
    { 
     var height = $(window).height(); 
     var scrollTop = $(window).scrollTop(); 
     var dHeight = $(document).height(); 

     if(height + scrollTop >= dHeight - 10) 
     { 
      alert("loaded is " + vloaded + " and filter is " + vfilter); 

      // an AJAX request instead of a POST request 
      $.ajax 
      (
      { 
       type: "POST", 
       url: "/organizer/getMore", 
       data: { filter: vfilter, loaded: vloaded }, 
       dataType: "html", 
       success: function(responseText, textStatus, XHR) 
       { 
       // select the element with the ID grid and insert the HTML 
       $("#grid").html(responseText); 
       } 
      } 
     ); 

      // global variable 
      vloaded +=30; 

     } // if 
    } 

); // on 

} // ready 
+0

使用'$(document).ready()',警報不再彈出,並且在Firefox中由於某種原因混亂了頁面的佈局。 – jaimerump

+0

每當Ajax啓動時,它似乎都會複製我的頁腳,它位於一個單獨的文件中並由Zend_Layout插入。而在Firefox上,它從頂部而不是底部開始以10個像素點擊。 – jaimerump

+0

你有一個我可以看到的面向網絡的URL嗎? – efesar