如何更改 localStorage 项中的单个值?
我正在尝试更改本地存储中的值。此项是复选框的状态。我希望,每次选中复选框时都将该复选框的值设置为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
setItem
采用两个参数:或更适合您的情况:
最佳实践:
这是一个示例:http: //jsfiddle.net/F8sF2/
编辑:从你的小提琴中,你需要更改:
更新
http://jsfiddle.net/CC5Vw/1/
setItem
takes two parameters:or more likely for your case:
Best Practices:
Here's an example: http://jsfiddle.net/F8sF2/
EDIT: from you fiddle, you need to change:
to
UPDATED http://jsfiddle.net/CC5Vw/1/
试试这个
localStorage
只是 JavaScript 对象,您可以将它们视为关联数组。所以你可以像这样使用它们。设置值
获取值
Try this
localStorage
is nothing but JavaScript object you can treat them as associate array. So you can use them like this.To set value
To get value