如何更改本地存储中复选框的单个值?
我正在尝试更改本地存储中的值。此项是复选框的状态。我希望,每次选中复选框时都将该复选框的值设置为 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.
}
});
这是我的本地存储的样子: 我希望有人能帮助我。我已经为此工作了几天了... 这是一个小提琴: http://jsfiddle.net/CC5Vw/7/
非常感谢
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...
Here is a fiddle for it: http://jsfiddle.net/CC5Vw/7/
Thanks alot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个: http://jsfiddle.net/Rh4Au/6/ 效果很好..
更改http://jsfiddle.net/CC5Vw/7/ 到 http://jsfiddle.net/Rh4Au/6/
第 30 行:
+ "
”
i
更改为j
因为 i 是长度,j 是迭代器。第 34 行后插入:
代码从
localStorage
加载数据并选中选中的复选框。tempasd 是一个临时变量,存储复选框的当前 ID 编号(因为它们并不总是升序。
从第 99 行更改为 117:
我
需要循环,因为它需要将事件委托给每个复选框,并且当未选中时,事件必须将
localStorage
设置为false
顺便说一句,
.parent().parent()
是不必要的。现在因为我固定在第 30 行。look at this: http://jsfiddle.net/Rh4Au/6/ works well..
changes http://jsfiddle.net/CC5Vw/7/ to http://jsfiddle.net/Rh4Au/6/
line 30:
+ "<div class='checkbox'><input type='checkbox' class='test' id='status-" +
i
+ "'></div>"
i
chaged toj
because i is the length and j is the iterator.insertion after line 34:
The code loads the data from
localStorage
and check in the checked checkboxes.The
tempasd
is a temporary variable store the current ID num of the checkbox (because they not always come ascending.changes from line 99 to 117:
to
i need the loop because it needs to delegate an event to each checkbox, and also the event must set the
localStorage
tofalse
when it gets unchecked.btw the
.parent().parent()
is unnecesarry now because i fixed at line 30.