javascript - 错误: Cannot read property 'checked' of undefined

标签 javascript html

我试图将选中的复选框的结果存储在本地存储中,但是当我尝试保存时,出现上述错误。

不太确定什么是未定义的,因为我在代码中看到了该属性。 代码如下:

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

最佳答案

问题是您在此处覆盖了 checkboxes(表单中复选框的原始集合)的值:

}else{
    checkboxes = "No"
}

这是循环的内部...循环遍历复选框。它现在正在迭代字符串“No”中的字符,而不是您最初检索的元素集合。该字符串只有 2 个字符长,而您的原始循环有一个存储在 j 中的 checkboxes.length 缓存值(5 个元素),该值不会在每次迭代时更新,这意味着它将循环遍历 i = 1,访问第三、第四和第五索引,这些索引现在未定义。

另一个“问题”是,在您的 storeData 函数中,您正在调用 getCheckboxValue 但没有将其存储在任何地方......然后您引用了一些 checkboxes 稍后变量 - item.features = ["features",checkboxes];。您需要存储结果,然后使用该变量。这是一个似乎适用于某些假设 HTML 的演示:

var $ = function (id) {
    return document.getElementById(id);
};

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 + " is checked");
        } else {
            //checkboxes = "No";
            console.log(checkboxes[i].value + " is not checked");
        }
    }
};

var storeData = function () {
    var id = Math.floor(Math.random()*10000001);
    var checkedBoxes = getCheckboxValue();
    var item = {};
    item.brand = ["Brand",$("brand").value];
    item.model = ["Model",$("model").value];
    item.comments = ["comments",$("comments").value];
    item.features = ["features",checkedBoxes];

    //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, false);

演示: http://jsfiddle.net/jmUXY/1/

关于javascript - 错误: Cannot read property 'checked' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17706058/

相关文章:

javascript - 如何在同一位置的下一行中找出td的索引

javascript - 使用 Javascript 生成的表单不会提交

javascript - 如何触发底层图片的onClick?

javascript - 将标签文本值获取到 javascript 变量中

javascript - 在 redux 中,当为调度编写 thunk 时, "next"和 "store.dispatch"之间有什么区别?

javascript - 查找文件并打印完整路径

javascript - 使用复选框更新 MySQL 值

javascript - JavaScript 中带有 onload 事件的竞态条件

javascript - 从 sigmaJS 图中删除前缀

javascript - javascript 错误 500 内部服务器错误