简体   繁体   English

存储动态创建的复选框值

[英]storaging of dynamically created checkbox value

So I have a todo list-like feature on one of my chrome extensions and I'm just not managing to get the checkbox to store the values. 因此,在我的一个chrome扩展程序中,我具有类似待办事项列表的功能,而我只是没有设法获得用于存储值的复选框。

I'm taking a bit of an unconventional approach in which I have a textbox and when I press enter, the textbox value creates my first todolist element in my div, then I send the textbox value to a textarea where I store all my todolist elements separated by a \\n . 我采用一种非常规的方法,其中有一个textbox ,当我按Enter键时, textbox值在div中创建了我的第一个todolist元素,然后将textbox值发送到存储所有Todolist元素的textarea中以\\n分隔。 Then when I reload the page, my stored textarea separates the values by new line and outputs them in a div. 然后,当我重新加载页面时,我存储的textarea用换行符分隔值并将它们输出到div中。 Probably not the best way of doing something like this, but it has worked for me thus far. 可能不是执行此类操作的最佳方法,但到目前为止,它已经对我有效。

Anyway, now I'm trying to store the checkbox value of dynamically created checkboxes. 无论如何,现在我正在尝试存储动态创建的复选框的复选框值。 So I assign each checkbox an id of i in which i is < textareaLines . 因此,我为每个复选框分配了一个i的ID,其中i< textareaLines I can't seem to figure out how to store the checkboxes dynamically, I know how to store them individually but this seems harder to me. 我似乎无法弄清楚如何动态存储复选框,我知道如何单独存储它们,但对我来说似乎很难。

Here is my code for setting my synced storage: 这是用于setting同步存储的代码:

var todolines = $('#todoListSave').val().split('\n');
        for(var i = 0;i < todolines.length;i++){
        console.log(i);
        var isTodoListChecked = document.getElementById(i).checked;
        chrome.storage.sync.set({
             todoList: isTodoListChecked
        });
          console.log(isTodoListChecked); 
        }

and here is my code for getting my synced storage: 这是getting同步存储的代码:

chrome.storage.sync.get('todoList', function (result) {
        todoList = result.todoList;
        console.log(todoList);
});

Any ideas? 有任何想法吗? Perhaps I'm just really over thinking it, but I can't seem to figure it out. 也许我只是真的想得太过分,但我似乎无法弄清楚。

Thanks. 谢谢。

My guess is that since the checkbox is dynamically generated, the line 我的猜测是,由于该复选框是动态生成的,因此该行

var isTodoListChecked = document.getElementById(i).checked;

will not work. 不管用。 You will need to call the jquery .on() function the checkbox value. 您将需要调用jquery .on()函数的复选框值。

$(document).on('checked', '#'+i, function(e){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM