如何更改 localStorage 项中的单个值?

发布于 2024-12-02 04:15:10 字数 859 浏览 0 评论 0原文

我正在尝试更改本地存储中的值。此项是复选框的状态。我希望,每次选中复选框时都将该复选框的值设置为 true 或 false。我尝试了很多方法,直到我意识到不使用 JSON 就无法更改值。

要添加我使用的值:

localStorage.setItem("status-" + i, $status.is(":checked"));

并删除我使用:

var parentId = $this.parent().attr('id');
localStorage.removeItem("'" + parentId + "'");

现在要更改我尝试的值:

$itemList.delegate("#status-" + i, 'click', function(e) {

                var $this = $(this);
                var parentId = this.parent().attr('id');            

                if ($this.is(":checked")) { 

                    localStorage.setItem("'" + parentId + "'".val("fdgsdagf"));
                    // Note that alert here works.

                }
});

这是我的本地存储的样子: 我希望有人能帮助我。我已经研究了几天了......

非常感谢

I'm trying to change a value inside localstorage. This item is the status of a checkbox. I want, everytime that a checkbox is checked to set the value to true or false of that checkbox. I tried many ways until I realized that there is no way you can change a value without using JSON.

To add the value I use:

localStorage.setItem("status-" + i, $status.is(":checked"));

and to delete I use:

var parentId = $this.parent().attr('id');
localStorage.removeItem("'" + parentId + "'");

Now to change the value I tried:

$itemList.delegate("#status-" + i, 'click', function(e) {

                var $this = $(this);
                var parentId = this.parent().attr('id');            

                if ($this.is(":checked")) { 

                    localStorage.setItem("'" + parentId + "'".val("fdgsdagf"));
                    // Note that alert here works.

                }
});

This is how my local storage looks like:

I hope someone could help me. I've been working on it for few days...

Thanks alot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-12-09 04:15:10

setItem 采用两个参数:

localStorage.setItem('status-1', "'" + parentId + "'".val("fdgsdagf"));

或更适合您的情况:

localStorage.setItem(parentId, "fdgsdagf");

最佳实践:

localStorage.setItem('key', JSON.stringify(value));
JSON.parse(localStorage.getItem('key'));

这是一个示例:http: //jsfiddle.net/F8sF2/

编辑:从你的小提琴中,你需要更改:

var parentId = this.parent().attr('id');    

更新

var parentId = $this.attr('id');    

http://jsfiddle.net/CC5Vw/1/

setItem takes two parameters:

localStorage.setItem('status-1', "'" + parentId + "'".val("fdgsdagf"));

or more likely for your case:

localStorage.setItem(parentId, "fdgsdagf");

Best Practices:

localStorage.setItem('key', JSON.stringify(value));
JSON.parse(localStorage.getItem('key'));

Here's an example: http://jsfiddle.net/F8sF2/

EDIT: from you fiddle, you need to change:

var parentId = this.parent().attr('id');    

to

var parentId = $this.attr('id');    

UPDATED http://jsfiddle.net/CC5Vw/1/

吖咩 2024-12-09 04:15:10

试试这个

localStorage.setItem(parentId, "fdgsdagf");

localStorage 只是 JavaScript 对象,您可以将它们视为关联数组。所以你可以像这样使用它们。

设置值

localStorage[parentId] = "fdgsdagf";

获取值

var status = localStorage[parentId];

Try this

localStorage.setItem(parentId, "fdgsdagf");

localStorage is nothing but JavaScript object you can treat them as associate array. So you can use them like this.

To set value

localStorage[parentId] = "fdgsdagf";

To get value

var status = localStorage[parentId];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文