2012-07-07 34 views
0

好了,所以我試圖加載數據,並移動到另一個頁面一次,我在我的index.html點擊搜索按鈕從MySQL獲得的jquerymobile數據只有當我刷新頁面

這是我的搜索按鈕

<a href="results.html" data-role="button" data-icon="search" 
data-iconpos="notext">search</a> 

雖然它裝載我希望頁面運行該功能,並獲取數據

 $(function() { $.getJSON("API.php", { 
     command: "getBusiness", 
     orig_lat: myPos.lat, 
     orig_long: myPos.lon, 
     distance: 0.05 }, 

     function (result) { 
     $("#locations").html(""); 
     for (var i = 0; i < result.length; i++) { 
      $("<a href='business.html?ID=" + result[i].id + "&bsnName=" + "'> 
      <div>" + result[i].bsnName + " " + (parseInt(result[i].distance * 1000)) 
      "</div></a>").appendTo("#locations");}});}); 

加載頁面時沒有DB只有當我打刷新它顯示我resul t

我不確定這裏有什麼問題,我不應該使用getJSON嗎?我見過人們在談論.Ajax()和getJSON()是一樣的嗎?

有沒有更好的主意,如何移動到另一個頁面,同時從數據庫抓取數據到你要加載到jquerymobile上的頁面?

我試着用使用相同的功能的onclick它工作時,我給它一個div

<link rel="stylesheet" href="styles/jquery.mobile.structure-1.1.0.min.css" /> 
    <link rel="stylesheet" href="styles/jquery.mobile.theme-1.1.0.min.css" /> 
    <link rel="stylesheet" href="styles/my.css" /> 
    <script src="scripts/jquery-1.7.2.min.js"></script> 
    <script src="scripts/jquery.mobile-1.1.0.min.js"></script> 
    <script src="scripts/cordova-1.8.1.js"></script> 

     <script> 

      // Wait for Cordova to load 
      // 
      document.addEventListener("deviceready", onDeviceReady, false); 

      var watchID = null; 
      var myPos = { lat: 32.0791, lon: 34.8156 }; 

      // Cordova is ready 
      // 
      function onDeviceReady() { 
       // Throw an error if no update is received every 30 seconds 
       var options = { timeout: 10000 }; 
       watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 
      } 

      // onSuccess Geolocation 
      // 
      function onSuccess(position) { 
       var element = document.getElementById('geolocation'); 
       //myPos.lat=position.coords.latitude; 
       //myPos.lon=position.coords.longitude;  

       element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' + 
          'Longitude: ' + position.coords.longitude + '<br />' + 
          '<hr />' + element.innerHTML; 
      } 

      // onError Callback receives a PositionError object 
      // 
      function onError(error) { 
       alert('code: ' + error.code + '\n' + 
       'message: ' + error.message + '\n'); 
      } 
+0

錯誤消息?如果有... – 2012-07-07 15:20:19

+0

有沒有錯誤味精,當我刷新它的工作頁面,但不 – Adam 2012-07-07 15:41:00

回答

0

基本上當jQuery Mobile的加載第一或索引頁面加載它整個頭部休息部分(Javascript,CSS等)和正文部分。但是當用戶點擊一個jQuery Mobile驅動站點中的鏈接時,導航系統的默認行爲是使用該鏈接的href來制定一個Ajax請求(而不是允許瀏覽器的默認鏈接行爲請求整個頁面加載時的href )。當Ajax請求消失時,框架將收到其整個文本內容,但它只會注入響應body元素的內容。

這個問題可以有多種解決方案,例如

  • 構建jQuery Mobile站點時最簡單的方法是在每個頁面的頭部引用同一組樣式表和腳本。

  • 通過在鏈接中使用屬性data-ajax =「false」鏈接沒有Ajax,此屬性將加載沒有Ajax和動畫的下一頁,以便加載頭部和主體部分。

  • 如果您需要加載特定頁面的特定腳本或樣式,建議將該邏輯綁定到pageInit,例如「#aboutPage」是id =「aboutPage」屬性。

    $(document).delegate("#aboutPage", "pageinit", function() { 
        //you can place your getJson script here. that will execute when page loads 
        alert('A page with an ID of "aboutPage" was just created by jQuery Mobile!'); 
    }); 
    

所以你的情況更好的解決方案是綁定你的Ajax調用或其他particuler腳本pageinit事件。

您可以從這些jQuery Mobile文檔頁面獲得幫助。

http://jquerymobile.com/demos/1.1.0/docs/pages/page-links.html

http://jquerymobile.com/demos/1.1.0/docs/pages/page-scripting.html

+0

之前,我嘗試使用您建議的代碼,它的工作,但我不得不添加data-ajax =「false」到我的標籤,並我的後退按鈕,所以每次我進入我的鏈接,它會加載JS代碼,有沒有辦法讓我的頁面保持Ajax?我應該用一個外部JS文件加載所有這些函數嗎? – Adam 2012-07-08 03:58:29

+0

這也解釋了爲什麼當我試圖加載一個地理位置函數不在索引頁面時,它創建了這個問題 – Adam 2012-07-08 04:27:35