数组的动态数组
Dynamic array of arrays
我正在创建一个在线测试页面,并希望捕获错误的答案并在另一个数组中推送。我的代码如下所示。我做错了什么?提前谢谢。
var ar, wqs, wqpos = 0;
var oneq = new Array(5);
function renderQuestion()
{
test = _("test");
if(pos >= ar.length)
{
_("test_status").innerHTML = "Test Completed";
pos = 0;
correct = 0;
wqpos = 0;
return false;
}
question = ar[pos][1];
..... rest of code
<input type='radio' name='choices' value='A'> "+chA+"<br>;
...... rest of code
<button onClick='checkAnswer()'>Submit Answer</button>;
}
function checkAnswer()
{
choices = document.getElementsByName("choices");
for(var i=0; i<choices.length; i++)
{
if(choices[i].checked)
{
choice = choices[i].value;
}
}
if(choice == ar[pos][5])
{
correct++;
}
else
{
oneq = [ar[pos][1], ar[pos][2], ar[pos][3], ar[pos][4], ar[pos][5]];
问题是下面两行
wqs[wqpos] = [oneq[0], oneq[1], oneq[2], oneq[3], oneq[4]];
wqpos++;
}
pos++;
renderQuestion();
}
我不明白你的问题,如果我不能解决它。但是,我读了你的代码,我认为方法renderQuestion()有问题。因为:
var ar, wqs, wqpos = 0; // ar is undefined
所以,在方法renderQuestion()检查:
if(pos >= ar.length) // ar.length is error: Uncaught TypeError, because ar is undefined
我认为你应该为变量设置默认值。
谢谢。
相关文章:
- 如何向JSON数组动态添加属性
- Javascript数组动态
- 使用RequireJS从数组动态加载模块
- 在主要的JavaScript引擎中,在JavaScript关联数组(动态对象属性)中检索/插入的复杂性是多少
- 基于PHP数组动态附加表单
- jQuery:从带有 for 循环的数组动态构建表单
- JSon 数组动态列填充 AngularJs
- 使用 Javascript 创建变量数组(动态,下拉)
- 从数组动态构建表
- Ext 3.4-如何使用本地数组动态填充组合框
- jquery将数组动态地输入到slug
- 用字符串和字符串数组动态填充json对象
- 如何在Meteor中使用数组动态渲染多个模板
- 如何从json字段数组动态创建一个没有jquery的表单
- 用数组动态填充多维数组
- Jquery数组动态初始化
- 根据json创建的2d数组动态填充表
- Knockout:基于未知长度的数组动态创建可观察对象
- 使用数组动态注册事件
- jQuery多维数组(动态键)-不能设置属性undefined