Javascript:TypeError 变量未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11586064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: TypeError variable is undefined
提问by mrdeived
I am currently building a small web application with similar functionality across all modules. I want to code small generic functions so that all programmers next to me, call these functions and these functions return necessary but important data for them to implement their functionality. In this example, I am trying to deal with the typical "choose true or false" exercise. So from the template.php they call this function:
我目前正在构建一个在所有模块中具有类似功能的小型 Web 应用程序。我想编写小的通用函数,以便我身边的所有程序员调用这些函数,这些函数返回必要但重要的数据,以便他们实现其功能。在这个例子中,我试图处理典型的“选择真或假”练习。所以从 template.php 他们调用这个函数:
function checkAnswers(){
var radiobuttons = document.form1.exer1;
var correctAnswers = answers(); //this is an array of string
var checkedAnswers = checkExerciseRB(radiobuttons, 2, correctAnswers);
for(i=0; i<checkedAnswers.length; i++){
alert(checkedAnswers[i]);
}
}
Function checkExerciseRB is my generic function, it is called from checkAnswers.
函数 checkExerciseRB 是我的通用函数,它是从 checkAnswers 调用的。
function checkExerciseRB(rbuttons, opciones, correct){
var answers = new Array();
var control = 0;
for(i=0; i<rbuttons.length; i++){
var noPick="true";
for(j=0; j<opciones; j++){
if(rbuttons[control+j].checked){
if(rbuttons[control+j].value==correct[i]){
answers[i]= 1;
noPick="false";
break;
}
else{
answers[i]=2;
noPick="false";
break;
}
}
}
if(noPick=="true")
answers[i]=0;
control=control+opciones;
}
return answers;
}
It works great but while looking at my favorite browsers (FireFox, Chrome) error log it says:
它工作得很好,但在查看我最喜欢的浏览器(FireFox、Chrome)错误日志时,它说:
TypeError: rbuttons[control + j] is undefined
Any clue on how to deal with this matter?
关于如何处理这个问题的任何线索?
回答by Jason Orendorff
This probably means that control + j
is greater than or equal to the length of the array rbuttons
. There's no such array element as rbuttons[control + j]
.
这可能意味着control + j
大于或等于数组的长度rbuttons
。没有像rbuttons[control + j]
.
You should learn how to use the JavaScript debugger in your favorite browsers! Debuggers are great. They let you watch this code run, line by line, as fast or as slow as you want, and watch how the value of control
changes as you go.
您应该学习如何在您喜欢的浏览器中使用 JavaScript 调试器!调试器很棒。它们让您可以随心所欲地一行一行地观察这段代码的运行速度,并观察其值的control
变化情况。
You'll watch it, and you'll think “Oh! Thatline of code is wrong!”
你会看着它,你会想“哦!那行代码是错误的!”
回答by MStodd
You're looping through rbuttons.length
times, but in each loop you're adding 2 to control
. Using control
to index your array, you're going to run past the end.
您正在循环rbuttons.length
,但在每个循环中,您都将 2 添加到control
. 使用control
索引您的阵列,你要过去的结束运行。
回答by Jason L.
Does the index specified by control + j exist in the array? i.e: If that evaluates to 4, is there at least 5 items in the array?
control + j 指定的索引是否存在于数组中?即:如果计算结果为 4,数组中是否至少有 5 个项目?
Also, you should be using var i, var j, etc inside your for loop. Without it your variables are leaking into the scope this code is executed in (most likely the global scope, and that's not good) :)
此外,您应该在 for 循环中使用 var i、var j 等。没有它,您的变量会泄漏到执行此代码的范围中(很可能是全局范围,这不好):)