项目重复onpageshow

时间:2013-11-19 14:33:14

标签: jquery

我必须在页面中搜索本地存储中的变量并相应地创建按钮。第一次加载页面时加载它没有错误,但只要我点击指向该页面的锚点按钮就会创建重复的内容。

$('#currentSamplePage').on('pageshow',function(){
    var station = 0;
    for(i=0;i<ls.length;i++){
        if(ls.key(i).match(stationSetContainerRegex)){
            var query = ls.key(i);
            var data = query.split('&');
            if(data[0] !== station){
            station = data[0];
            $('#currentSamples').append(createButton(data[0],'#multiSet',data[0]));
            console.log(station);
            }else{}


        }
    }

});

但我想要的是每次访问此页面时都看到内容是最新的。 The first time I access the page The second time I acces the page and will repeat to the Nth time I access the page. 这个fiddle可以帮助您查看它,但由于某种原因,我的localStorage无法正常工作。注释可以让您知道本地存储中的输入数据是什么。

2 个答案:

答案 0 :(得分:1)

每次显示页面时都会引发

pageshow,您希望绑定到pageinit,这只是在第一次初始化页面时引发的:

$('#currentSamplePage').on('pageinit',function(){

参考:http://api.jquerymobile.com/pageinit/

答案 1 :(得分:1)

我的方法是克里斯哈迪建议删除所有按钮并再次制作它们。

$('#currentButton').click(function(e){
        $('#currentSamples').empty();
        var station = 0;
        for(i=0;i<ls.length;i++){
            if(ls.key(i).match(stationSetContainerRegex)){
                var query = ls.key(i);
                var data = query.split('&');
                if(data[0] !== station){
                station = data[0];
                $('#currentSamples').append(createButton(data[0].substring(1),'#multiSet',"getJsonFromLocalStorage('"+query+"')",data[0]));console.log(station);
                }else if(!ls.key(i).match(stationSetContainerRegex)){$('#currentSamples').append('<span> There are no samples </span>')}


            }
        }
    });

希望这会有所帮助,以及CREDITS:Chris Hardie的想法