我正在尝试将已选中复选框的结果存储在本地存储中,但是当我尝试保存时,我得到上面提到的错误.
不太确定什么是未定义的,因为我在代码中看到了属性.这是代码:
var getCheckboxValue = function (){
var checkboxes = document.forms[0].features;
for(var i=0, j=checkboxes.length; i
if(checkboxes[i].checked){
console.log(checkboxes[i].value);
//featuresValue = $("features").value();
}else{
checkboxes = "No"
}
}
}
var storeData = function (){
var id = Math.floor(Math.random()*10000001);
//getSelectedRadio();
getCheckboxValue();
var item = {};
item.brand = ["Brand",$("brand").value];
item.model = ["Model",$("model").value];
item.comments = ["comments",$("comments").value];
//item.acoustic = ["acoustic", acousticValue];
item.features = ["features",checkboxes];
//Save data into local storage: Use Stringify to convert our object to a string
localStorage.setItem(id,JSON.stringify(item));
alert("Contact Saved!");
}
//set link & submit click events
var save = $("button");
save.addEventListener("click", storeData);
这是相关的html:
Features:- Cutaway
- Finished
- Inlay
- Wide Neck
- Left-handed
或者,这是github上完整代码的链接: