javascript
  • jquery
  • xml
  • ajax
  • 2012-02-19 131 views 1 likes 
    1

    我有此位的代碼而使用顯示從XML提要一些地方AJAX:AJAX - 在頁面刷新雙重結果

    它顯示正確,但
    $.ajax({ 
        type: "GET", 
        url: "testxml.xml", 
        dataType: "xml", 
        success: function(xml) { 
         $(xml).find("Country[Name='"+localStorage.ArrivalCountry+"']").find('Destination').each(function(){ 
          var destinationName = $(this).attr('Name'); 
          $('<a class="listItem" href="#" id="'+destinationName+'">'+destinationName+x+'<div class="arrow"></div></a>').appendTo('#destinationList'); 
         }); 
    
        } 
    }); 
    

    第一次,如果我刷新頁面,它會顯示每個結果兩次。如果我再次執行,則會顯示三次,依此類推。

    +0

    那麼你的Ajax調用返回???給我們一個例子.. – 2012-02-19 16:27:45

    回答

    1

    您需要empty()#destinationList以前的AJAX調用數據添加更新之前設置:

    $.ajax({ 
        type: "GET", 
        url: "testxml.xml", 
        dataType: "xml", 
        success: function(xml) { 
         $("#destinationList").empty(); // This will clear out all the previously appended 'a' elements 
         $(xml).find("Country[Name='"+localStorage.ArrivalCountry+"']").find('Destination').each(function() { 
          var destinationName = $(this).attr('Name'); 
          $('<a class="listItem" href="#" id="'+destinationName+'">'+destinationName+x+'<div class="arrow"></div></a>').appendTo('#destinationList'); 
         }); 
        } 
    }); 
    

    更多有關empty()

    +0

    他寫道他刷新頁面,所以由於Web應用程序的無狀態,它不需要它。如果我理解他是對的。 – gdoron 2012-02-19 16:28:21

    +0

    謝謝。解決了問題:D – Andrei 2012-02-19 16:29:11

    相關問題