我試圖將選中複選框的結果存儲在本地存儲中,但是當我嘗試保存時出現上述錯誤。錯誤:無法讀取屬性'檢查'未定義
不太清楚什麼是未定義的,因爲我看到了我的代碼中的屬性。 下面的代碼:
var getCheckboxValue = function(){
var checkboxes = document.forms[0].features;
for(var i=0, j=checkboxes.length; i<j; 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:
<li>Features:
<ul>
<li><input type="checkbox" name="features" value = "Cutaway" id="cutaway" /><label for="cutaway">Cutaway</label></li>
<li><input type="checkbox" name="features" value = "Finished" id="finished" /><label for="finished">Finished</label></li>
<li><input type="checkbox" name="features" value = "Inlay" id="inlay" /><label for="inlay">Inlay</label></li>
<li><input type="checkbox" name="features" value = "Wide Neck" id="wneck" /><label for="wneck">Wide Neck</label></li>
<li><input type="checkbox" name="features" value = "Left-handed" id="lhanded" /><label for="lhanded">Left-handed</label></li>
</ul>
</li>
另外,這裏是充分代碼的鏈接在GitHub上:
https://github.com/b80266/VFW-Project-2/blob/master/additem.html
https://github.com/b80266/VFW-Project-2/blob/master/main.js
什麼線是從這個來的?你做了什麼來調試呢? – djechlin
@GolezTrol爲什麼'checkboxes [i]'是'undefined'? – Ian
對不起,錯誤是它說「如果(複選框[i] .checked){」 – Steven07