2017-04-02 101 views
0

我有一個奇怪的問題與JQuery的移動。我創建了一個顯示帖子並正確顯示帖子的頁面。頁面顯示重複的帖子,我每次點擊按鈕

現在的問題是,每當我點擊後退按鈕並再次點擊帖子按鈕時,它會複製帖子並顯示。我點擊後退按鈕並點擊發布按鈕的次數,帖子相應地被複制並以倍數顯示。

例如,我有一個正在顯示的帖子。現在我點擊返回按鈕,然後點擊顯示帖子按鈕,這次,同一帖子顯示兩次。現在再次,如果我點擊返回按鈕,然後點擊顯示帖子按鈕,它現在相同的帖子顯示三次等等。

但是,如果我在每次後退按鈕點擊後刷新頁面,則它會正確顯示帖子。任何人都可以引導我在哪裏出錯?

這裏是代碼:

HTML頁面:

<div data-role="page" id="userpost"> 
    <div data-role="header" data-theme="b"> 
     <a class="back" class="button button-clear" data-rel="back" data-icon="back" href="#">back</a> 
     <p style="text-align: center">Posts History</p> 
    </div> 
    <div data-role="content"> 
     <p id="username"></p> 
     <div class="row cf" id="userPosts"> 
     </div> 

     <div data-role="footer" data-position="fixed" data-theme="b"> 
      <h6>&copy;</h6> 
     </div> 
    </div> 
</div> 

jQuery代碼:

function displayuserPosts() { 
    if (localStorage.login = "true") { 
     var username = localStorage.username; 
     $('#username').append("<p style='font-size:10px; color:red; text-align:center;'>Welcome: " + username + "</p>"); 
     var userPosts = "username=" + username + "&upost"; 
     $.ajax({ 
      type: "GET", 
      url: "../php-code2/uposts.php", 
      data: userPosts, 
      dataType: "json", 
      success: function (data) { 
       $.each(data, function (i, field) { 
        $("#userPosts").append('<p class="job cf">'+ field.title +'</p>'); 
       }); 
      } 
     }); 
    } else { 
     alert("Please login to post a Job"); 
     window.location.href = "#Main"; 
    } 
} 
+0

如果沒有您迄今製作的代碼,我們無法提供幫助 – LGSon

+0

現在更新爲代碼。通過此代碼,我非常能夠在頁面上顯示帖子標題。但是,一旦我點擊返回按鈕,然後嘗試點擊帖子按鈕,問題就開始了。然後它複製相同的內容並顯示兩次。這種情況下增加的次數,我點擊發布按鈕。但是,如果我刷新頁面,那麼它顯示正常。 – MGS

回答

0

在此行中:

$("#userPosts").append('<p class="job cf">'+ field.title +'</p>'); 

變化.append.html

「追加」意味着添加內容到元素中已有的內容(在這種情況下爲#userPosts)。




EDIT

好。那我們試試其他的東西吧。
您已經在使用本地存儲...

爲什麼在加載帖子時不設置布爾標誌?
和一個日期標誌知道它何時被加載...

我稱之爲一個標誌是一個變量,只用於記住一個狀態...用於比較。

所以這應該工作。
請注意,您可以使用more precise time來記住。

// It good to know todays date. 
var today = new Date().getDate(); // day of the month 

function displayuserPosts() { 

    // Executes only if logged in AND the posts not already loaded today. 
    if( localStorage.login == "true" 
     && localStorage.userpostsLoaded != "true" 
     && localStorage.userpostsWhen != today){ 

     var username = localStorage.username; 
     $('#username').append("<p style='font-size:10px; color:red; text-align:center;'>Welcome: " + username + "</p>"); 
     var userPosts = "username=" + username + "&upost"; 

     $.ajax({ 
      type: "GET", 
      url: "../php-code2/uposts.php", 
      data: userPosts, 
      dataType: "json", 
      success: function (data) { 
       $.each(data, function (i, field) { 
        $("#userPosts").append('<p class="job cf">'+ field.title +'</p>'); 
       }); 

       // Set some flags! 
       localStorage.userpostsLoaded="true"; 
       localStorage.userpostsWhen=today; 
      } 
     }); 

    }else{ 

    // If the Ajax request was avoided because of the login 
    if(localStorage.login != "true"){ 
     alert("Please login to post a Job"); 
     window.location.href = "#Main"; 
    } 
    } 
} 
+0

這不足以滿足我的要求。因爲我正在循環元素中的內容,這意味着我需要顯示多個結果。但是,當我更改爲.html時,它只顯示一個結果inteasd all。請建議 – MGS

+1

感謝Louys的回覆。我用另一種方式解決了這個問題我剛添加$('#userPosts')。HTML( '');在登錄憑證之後的函數開始,確保元素在加載前是空的,現在工作正常:-)。雖然接受你的答案。 :-)。謝謝! – MGS

相關問題