javascript
  • jquery
  • xml
  • ajax
  • 2012-02-19 130 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

    相关问题