【问题标题】:Iterating through Array of Objects to fill input fields遍历对象数组以填充输入字段
【发布时间】:2015-11-09 15:08:37
【问题描述】:

我目前正在研究一种 Google 表单的解决方法,它能够将所有输入存储在 cookie 中,以便用户可以在不同的时间继续调查。 目前,我能够存储所有问题(问题是一个对象,其中包含:周围 div 的 id、必需、使用 JSON.stringify() 在 cookie 中的用户输入。我还能够读取和解析得到我的 cookie所有问题对象的数组。

现在我想填写所有字段或检查所有具有值的单选按钮。 我的问题是,内部 for 循环只执行 2 次迭代,但应该执行 18 次。您知道可能出了什么问题吗?

function restoreInputs() {
    // number of stored cookies
    var countCookies = 27;
    console.log(countCookies);
    // iterate through all cookies
    for (var i = 1; i < countCookies + 1; i++) {
        var cookiename = "answer" + i;
        // get content of cookie (is array of objects)
        var answer = checkCookie(cookiename);
        // iterate through content      
        for (var j = 0; j < answer.length; j++) {
            // get value of object
            var val = answer[j].value;
            // get the input field (textarea or radio button)
            var x = document.getElementById(answer[j].n).getElementsByTagName('input');
            // if input is radio, then check the one at position stored in value of object
            if (x[j].type === "radio") {
                x[val].checked = true;
                // if textarea set its value to the one stored in object value
            } else {
                x[j].value = val;
            }
            console.log("j: " + j);
        }

    }
    console.log(i);
}

【问题讨论】:

  • 为什么要做18而不是2?您显示的代码示例没有向我们提供有关 checkCookie() 中发生的事情的信息,有人可以理解为什么 answerlength 为 2。您是否尝试过 console.log(answer) 以查看您获得的价值以及与您的预期相比?
  • var answer = checkCookie(cookiename) 的内容是什么?我的第一个猜测是长度不是 18,但需要查看数据才能确定。

标签: javascript arrays for-loop


【解决方案1】:

我找到了解决方案。问题是我忘记了 for 循环,因为 var x = document.getElementById(answer[j].n).getElementsByTagName('input'); 可能返回多个元素。所以这里是解决方案:

function restoreInputs() {
// number of stored cookies
var countCookies = 27;
console.log(countCookies);
// iterate through all cookies
for (var i = 1; i < countCookies + 1; i++) {
    var cookiename = "answer" + i;
    // get content of cookie (is array of objects)
    var answer = checkCookie(cookiename);
    // iterate through content      
    for (var j = 0; j < answer.length; j++) {
        // get value of object
        var val = answer[j].value;
        // get the input field (textarea or radio button)
        var x = document.getElementById(answer[j].n).getElementsByTagName('input');
        // if input is radio, then check the one at position stored in value of object
        for (var k = 0; k < x.length; k++) {

            if (x[k].type === "radio") {

                x[val].checked = true;
                // if textarea set its value to the one stored in object value
            } else {
                x[k].value = val;
            }
        }
        console.log("j: " + j);
    }
}
console.log(i);

}

【讨论】:

  • 可能是您的 checkCookie 返回未定义。使用内部循环上的断点调试代码。您应该记住的重要一点是 Javascript 变量是函数范围的,而不是块范围的。
  • @ctarabusi 非常有用的评论,谢谢!我现在使用if (Object.prototype.toString.call( answer ) === '[object Array]' ) 在循环遍历它之前检查答案是否是一个数组。因此,如果未定义答案,它不会崩溃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
相关资源
最近更新 更多