javascript - jquery-mobile ListView -> 刷新事件

标签 javascript jquery jquery-mobile cordova

我看过一些与此类似的帖子,所以如果这是重复的,我提前表示歉意。

我在提取一些数据时有这个代码,但我似乎无法弄清楚为什么我的列表没有被刷新......有任何可能的解决方案吗?这是我的 JS。

/* VARIABLES
-------------------------------------------------------------------- */
var serviceUrl = "http://localhost/app/services/";
var serviceToLoad = "homeList.php";

var home;
var pageId    = "#homePage";
var contentId = "#homeList";


/* JQUERY
-------------------------------------------------------------------- */
$(pageId).live('pageshow', function(event){
    getHomeList();
});


/* FUNCTION SET
-------------------------------------------------------------------- */
function getHomeList() {

    $.getJSON(serviceUrl + serviceToLoad, function(data) {

        // Remove all content first
        $(contentId +' li').remove();

        // Load Data
        $.each(data.items, function(index, list){
            $(contentId).append('<li><a href="#">' + list.name + '</a></li>\n');
        });

    });

    // Reload View
    $(contentId).listview('refresh');
}

这是我的 HTML 页面

<div id="homePage" data-role="page" >
    <div data-role="header" data-position="fixed"><h1>Home</h1></div>

    <div data-role="content">
         <ul id="homeList" data-role="listview"></ul>
    </div>
</div>

任何帮助将不胜感激!

最佳答案

Ajax 调用是异步,因此,请将刷新 ListView 放在 jSON 调用中!

/* VARIABLES
-------------------------------------------------------------------- */
var serviceUrl = "http://localhost/app/services/";
var serviceToLoad = "homeList.php";

var home;
var pageId    = "#homePage";
var contentId = "#homeList";


/* JQUERY
-------------------------------------------------------------------- */
$(pageId).live('pageshow', function(event){
    getHomeList();
});


/* FUNCTION SET
-------------------------------------------------------------------- */
function getHomeList() {

$.getJSON(serviceUrl + serviceToLoad, function(data) {

    // Remove all content first
    $(contentId +' li').remove();

    // Load Data
    $.each(data.items, function(index, list){
        $(contentId).append('<li><a href="#">' + list.name + '</a></li>\n');
    });

    // Reload View
   $(contentId).listview('refresh')
});
}

关于javascript - jquery-mobile ListView -> 刷新事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9391407/

相关文章:

javascript - 使用 Rollup 编译 Sass 文件

javascript - 将数据从一张纸过滤到另一张纸

javascript - Bootstrap 轮播位置

javascript - 在纵向模式 iPad/iPhone 中禁用 Jquery

javascript - 如何将参数传递给PhoneGap数据库事务executeSql函数

javascript - 从以键/值结构格式化的数组中检索值

javascript - 如何在页面中添加HTML模板?

javascript - Jquery,在文本区域中格式化

javascript - 展开/折叠 jQuery ui mobile 中的所有列表元素

jquery - iOS Cordova 用户提示多次显示