如何使用javascript在本地存储中存储多个复选框值?

如何使用javascript在本地存储中存储多个复选框值?,javascript,local-storage,storage,local,Javascript,Local Storage,Storage,Local,尝试存储复选框的值,但我的本地chrome存储应用程序上似乎没有弹出任何内容:( 兴趣: 教育目的 CCA积分 发展 设计 生意 注册 函数注册() { localStorage.setItem(“用户兴趣”+document.getElementByClassName(“light”).value,document.getElementByClassName(“light”).value); } 文档中没有名为getElementByClassName的方法,只有getElementsByCl

尝试存储复选框的值,但我的本地chrome存储应用程序上似乎没有弹出任何内容:(

兴趣:
教育目的
CCA积分
发展
设计
生意 注册 函数注册() { localStorage.setItem(“用户兴趣”+document.getElementByClassName(“light”).value,document.getElementByClassName(“light”).value); }
文档中没有名为
getElementByClassName
的方法,只有
getElementsByClassName
返回节点集合,因此您需要将函数更改为类似以下内容:

function signup()
{
    //Declare constant variable to hold the elements with class light
    const lightEles = document.getElementsByClassName("light");

    //Declare an array to hold the values
    let eleValues = [];

    //Iterate the node collection and push each element's value into the array
    for(let x = 0; x < lightEles.length; x++){
        eleValues.push(lightEles[x].value);
    }

    //Set array to localStorage key
    localStorage.setItem("users_interests", eleValues);
}
函数注册()
{
//声明常量变量以使用类light保存元素
const lightEles=document.getElementsByClassName(“light”);
//声明一个数组来保存值
设elevatalues=[];
//迭代节点集合并将每个元素的值推送到数组中
for(设x=0;x
试试这个

函数注册()
{
var用户利益=[];
var els=document.getElementsByName(“用户兴趣”);
Array.prototype.forEach.call(els,函数(el){
如果(el.type==“复选框”&&el.checked){
用户兴趣推送(el.value);
}
//在这里做事
});
setItem(“用户兴趣”,JSON.stringify(用户兴趣));
log(JSON.parse(localStorage.getItem(“用户兴趣”));

}
没有名为
getElementByClassName
的文档方法,只有
getElementsByClassName
返回节点集合,请参见此处的文档()在本例中,我们都检查并将结果放入一个数组中,如果您想使用逗号分隔的返回,只需删除json.json返回数组示例-[a,b,c]而不是json(字符串)示例-a,b,c感谢您的澄清!
function signup()
{
    //Declare constant variable to hold the elements with class light
    const lightEles = document.getElementsByClassName("light");

    //Declare an array to hold the values
    let eleValues = [];

    //Iterate the node collection and push each element's value into the array
    for(let x = 0; x < lightEles.length; x++){
        eleValues.push(lightEles[x].value);
    }

    //Set array to localStorage key
    localStorage.setItem("users_interests", eleValues);
}