如何更改本地存储中复选框的单个值?

发布于 2024-12-02 05:50:20 字数 965 浏览 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.

                }
});

这是我的本地存储的样子: 我希望有人能帮助我。我已经为此工作了几天了... 这是一个小提琴: 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 技术交流群。

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

发布评论

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

评论(1

顾忌 2024-12-09 05:50:20

看看这个: http://jsfiddle.net/Rh4Au/6/ 效果很好..

更改http://jsfiddle.net/CC5Vw/7/http://jsfiddle.net/Rh4Au/6/


第 30 行:

+ "


i 更改为 j 因为 i 是长度,j 是迭代器。


第 34 行后插入:

    var tempasd = orderList[j].split("-")[1];
    if(localStorage.getItem("status-"+tempasd) == "true") {
        $("#status-"+j).get(0).checked = true
    }
    else {
        $("#status-"+j).get(0).checked = false
    }
}

代码从 localStorage 加载数据并选中选中的复选框。
tempasd 是一个临时变量,存储复选框的当前 ID 编号(因为它们并不总是升序。


从第 99 行更改为 117:

// ON STATUS CLICK   
console.log('i=',i,' j=',j, 'k=',k);


$itemList.delegate("#status-" + i, 'click', function(e) {  // ON CLICK
    var $this = $(this);
    var parentId = $this.attr('id');    
       
    if ($this.is(":checked")) {


        console.log('set ', parentId, 'to foo');
        localStorage.setItem(parentId, 'foo');
    //            localStorage.setItem("'" + parentId + "'".val("fdgsdagf"));
    //            localStorage.setItem($this.parent().attr('checked', true)); 
    //            window.location = "http://mail.aol.com"
    }

});

// ON STATUS CLICK   
console.log('i=',i,' j=',j, 'k=',k);
var tempxx;
for(tempxx = 0;tempxx < j; tempxx++) {
$itemList.delegate("#status-" + tempxx, 'click', function(e) {  // ON CLICK
    var $this = $(this);
    var parentId = $this.parent().parent().attr('id').split("-")[1];   


    

        console.log('set ', parentId, 'to foo');
        localStorage.setItem("status-"+parentId, $this.is(":checked"));
//            localStorage.setItem("'" + parentId + "'".val("fdgsdagf"));
//            localStorage.setItem($this.parent().attr('checked', true)); 
//            window.location = "http://mail.aol.com"

   
});

}

需要循环,因为它需要将事件委托给每个复选框,并且当未选中时,事件必须将 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 to j because i is the length and j is the iterator.


insertion after line 34:

    var tempasd = orderList[j].split("-")[1];
    if(localStorage.getItem("status-"+tempasd) == "true") {
        $("#status-"+j).get(0).checked = true
    }
    else {
        $("#status-"+j).get(0).checked = false
    }
}

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:

// ON STATUS CLICK   
console.log('i=',i,' j=',j, 'k=',k);


$itemList.delegate("#status-" + i, 'click', function(e) {  // ON CLICK
    var $this = $(this);
    var parentId = $this.attr('id');    
       
    if ($this.is(":checked")) {


        console.log('set ', parentId, 'to foo');
        localStorage.setItem(parentId, 'foo');
    //            localStorage.setItem("'" + parentId + "'".val("fdgsdagf"));
    //            localStorage.setItem($this.parent().attr('checked', true)); 
    //            window.location = "http://mail.aol.com"
    }

});

to

// ON STATUS CLICK   
console.log('i=',i,' j=',j, 'k=',k);
var tempxx;
for(tempxx = 0;tempxx < j; tempxx++) {
$itemList.delegate("#status-" + tempxx, 'click', function(e) {  // ON CLICK
    var $this = $(this);
    var parentId = $this.parent().parent().attr('id').split("-")[1];   


    

        console.log('set ', parentId, 'to foo');
        localStorage.setItem("status-"+parentId, $this.is(":checked"));
//            localStorage.setItem("'" + parentId + "'".val("fdgsdagf"));
//            localStorage.setItem($this.parent().attr('checked', true)); 
//            window.location = "http://mail.aol.com"

   
});

}

i need the loop because it needs to delegate an event to each checkbox, and also the event must set the localStorage to false when it gets unchecked.

btw the .parent().parent() is unnecesarry now because i fixed at line 30.

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