2012-03-14 75 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