试图存储复选框的值,但似乎没有任何东西弹出我的本地存储应用程序的chrome :(
< label id =‘interest’>兴趣:< / label>< input type =“checkbox”id =“…
试试这个
function signup() { var users_interests =[]; var els = document.getElementsByName("user_interest"); Array.prototype.forEach.call(els, function(el) { if (el.type == "checkbox" && el.checked) { users_interests.push(el.value); } // Do stuff here }); localStorage.setItem("users_interests",JSON.stringify(users_interests)); console.log(JSON.parse(localStorage.getItem("users_interests"))); }
没有这种文件的方法叫做 getElementByClassName 只要 getElementsByClassName 它返回一个节点集合,所以你需要将你的函数改为这样的:
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); }