2013-02-07 49 views
1

我希望這個AJAX調用在加載頁面時加載。現在我需要點擊刷新按鈕來加載它。在頁面加載時加載此AJAX調用

更新代碼:

$(document).on("pageinit", "#members", function() { 
$.ajax({ 
url: 'data.php', 
success: function(data) {  
$("#result").html(data).trigger('create'); 
$("#result").listview('refresh'); 
} 
}); 
}); 

它加載正確的,如果我在頁面中輸入URL,但是當我去不是通過我的index.php文件。

+4

jQuery的'.ajax()'函數_is_正在執行XMLHttpRequest調用。您是否嘗試過使用控制檯進行調試? – j08691

+0

不要在jQuery Mobile中使用document.ready。你需要的是'$(document).on(「pageinit」,「#pageid」,function(){/ *在這裏做ajax * /});' –

+0

@KevinB - 我錯過了jQuery移動提及的地方? – j08691

回答

2

首先,

$("document") 

應該

$(document) 

因爲沒有<document>元素,如果有的話,你就不會被要求他們.ready()

接下來,您看到此問題的原因是因爲$(document).ready()僅在每個整頁加載時觸發一次。在jQuery Mobile中更改爲新頁面不會觸發整頁加載,而是會使用ajax加載到新頁面中。 。爲了解決這個問題,jQuery Mobile的有一個稱爲是獲取獲取加載頁面上引發「pageinit」的事件這是你會怎麼給它綁定在你的情況:

前的jQuery 1.7

// yes, i know delegate is better, but the documentation specifically suggested using .live 
$("#pageid").live("pageinit",function(){ 
    $.ajax({ 
     url: 'data.php', 
     success: function(data) {  
      $("#result").html(data).trigger('create'); 
      $("#result").listview('refresh'); 
     } 
    }); 
}); 

後的jQuery 1.7

$(document).on("pageinit","#pageid",function(){ 
    $.ajax({ 
     url: 'data.php', 
     success: function(data) {  
      $("#result").html(data).trigger('create'); 
      $("#result").listview('refresh'); 
     } 
    }); 
}); 

原因刷新頁面的工作原理是,因爲當你刷新頁面,則$(document).ready()事件發生。

+0

'$(document).delegate(selector,events,data,handler); '。這應該被用來替代'live()'從'1.4.3'到Post'1.7'。 –

+0

謝謝。我嘗試了兩個版本都沒有成功:(我仍然需要刷新。 –

+0

如果我輸入頁面的URL,但是當我瀏覽我的'index.php'文件時,它會正確加載 –