在localStorage中存储来自listview的ID [英] store ID from listview in localStorage

查看:87
本文介绍了在localStorage中存储来自listview的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for(var i = 0; i< data.length ; i ++){
var garage = data [i];

$(#content-p)。append(
< ul data-role = \listview\>+
< li>< a href = \#\id = \submitID\>+
< h2>+ garage.location +< / h2>+
< p>< b>+ garage.description +< / b>< / p>+
< p>+ garage.address +< / p> +
< / a>< / li>+
< / ul>< br>
);

$(#submitID)。click(function(){
localStorage.setItem(garageID,garage.ID);
$ .mobile.changePage( #resP);
});

}

现在用户可以点击一个listview项目和ID从这个项目应存储在localStorage。



问题:它首先创建完整的列表视图与所有项目。如果我现在点击列表中的项目#2,我会从最后一项获得ID。

当没有参考或类似的东西时,该方法试图引用车库。你应该为特定的ID做点击方法,因为它每次都会被覆盖。我通过为一个类单击一个方法来修复它,但每个车库元素的ID都是它的实际车库ID。 http://jsfiddle.net/Fq3TF/1/



另外,只是一个提示,如果你正在构造一个将被插入HTML文档的字符串,你可以使用撇号而不是引号来避免转义。例如< a class ='submitID'> Hello World< / a>


I've a listview created by a loop:

for(var i = 0; i < data.length; i++) {
    var garage = data[i];

    $("#content-p").append(
        "<ul data-role=\"listview\">" +
                "<li><a href=\"#\" id=\"submitID\" >" +
                "<h2>"+garage.location+"</h2>" +
                "<p><b>"+garage.description+"</b></p>" +
                "<p>"+garage.address+"</p>" +
            "</a></li>" +
        "</ul><br>"
    );

    $("#submitID").click(function() {
        localStorage.setItem("garageID",garage.ID);
        $.mobile.changePage("#resP");
    });

}

Now the user can click on a listview item and the ID from this item should be stored in the localStorage.

Problem: It first creates the complete listview with all items. If I click now on item#2 from the list, I get the ID from the last item.

解决方案

I think the problem was that the method was trying to reference garage when there wasn't a reference, or something like that. And you should have made the click method for a specific ID, because it just kept getting overridden each time. I fixed it for you by making it one click method for a class, but the ID of each garage element is its actual garage ID.

http://jsfiddle.net/Fq3TF/1/

Also, just a tip, if you're constructing a string which is going to be inserted into an HTML document, you can use an apostrophe instead of a quote to avoid escaping. e.g. "<a class='submitID'>Hello World</a>".

这篇关于在localStorage中存储来自listview的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆