jquery - 清除 Ajax 结果

标签 jquery ajax listview

我在使网络服务调用正常工作时遇到问题。

我的成功调用将 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'); 
    }

谢谢

最佳答案

确保 imageTile 正在重置。我假设它是一个全局变量,如果是这样,您只需附加到它即可。

更改为:

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

应该这样做。或者,更好的是,如果没有理由将其设为全局变量,则将其设为局部变量:

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

关于jquery - 清除 Ajax 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9694111/

相关文章:

jquery - Bootstrap 提交按钮不提交

javascript - jQuery 自动完成不适用于克隆

javascript - 使用 Javascript 或 jQuery 启动和停止 Bootstrap 轮播

javascript - 用户向下滚动时加载内容

使用 GroupStyle 时 WPF 多页 Listview 打印

android - UIAutomator 基于索引点击 ListView

Android 从 listView onClick 获取正确的信息

php - 带锁定的用户友好视频审查

javascript - 将事件处理程序绑定(bind)到动态生成的 div 的问题

jquery - jQuery的empty()和remove()函数是否异步执行?