2012-03-14 79 views
2

我在使web服務調用正常工作時遇到問題。清除Ajax結果

我的成功電話將LI標籤列表添加到UL,目標是在進行另一個電話之前清除這些結果。現在,如果您要點擊通話按鈕,新的結果將被添加到列表中。

我使用.listview('refresh');應該這樣做,但它不適合我。

關於發生了什麼的任何想法?

的HTML

<div data-role="content" id=""> 

      <ul data-role="listview" id="Gallery" class="gallery"> 
      </ul> 

    </div> 

的JS

getAlbumImages = function (album) { 
     $.ajax({ 
      url: wcfServiceUrl + "GetMediaSource?AlbumId=" + album, 
      data: '{}', 
      type: 'GET', 
      contentType: "application/x-www-form-urlencoded", 
      async: true, 
      //crossBrowser: true, 
      dataType: 'jsonp', 
      //jsonpCallback: 'MediaSource', 
      timeout: 5000, 
      success: function (data, status) { 
       $.each(data, populateImageGrid); 
      }, 
      complete: function() { 
       $("ul#Gallery").listview("refresh"); 
      }, 
      error: function() { 
       alert('There was an error loading the data.'); 
      } 
     }); 
    } 
function populateImageGrid() { 
     var photoSource, thumbSource, title, description; 
     photoSource = this.ImageSource; 
     thumbSource = this.ThumbSource; 
     title = this.Title; 
     description = this.Description; 
     imageTile += "<li><a href='" + photoSource + "'><img src='" + thumbSource + "' alt='" + title + "' /></a></li>"; 
     console.log(imageTile); 
     $("ul#Gallery").html(imageTile); 
     //$("ul#Gallery").listview(); 
     // $("ul#Gallery").listview('refresh'); 
    } 

感謝

回答

1

確保imageTile處於復位狀態。我假設它是一個全局變量,如果是這樣,你只是追加它。

更改爲此:

success: function (data, status) { 
    imageTite = ""; 
    $.each(data, populateImageGrid); 
}, 

應該這樣做。或者,甚至更好的是,如果沒有理由使其成爲全局變量,則使其成爲局部變量:

var imageTile = "<li><a href='" + photoSource + "'><img src='" + thumbSource + "' alt='" + title + "' /></a></li>"; 
+0

謝謝。我採取了使用imageTile =「」的方法;在頁面更改。我想是在想什麼是需要做的。 – 2012-03-14 01:08:14