我使用钛函数返回数组。我的数组中有两个条目显示警报,但是当我在另一个js文件中访问返回的值时。它只在警报中显示一个值 这是我的代码(它在db.js中):
function quizfun() {
var dataArray=new Array();
var quizes = db.execute('select * from Quiz');
while (quizes.isValidRow()) {
var counter = 0;
dataArray[counter] = quizes.fieldByName('Quiz_Text');
quizes.next();
alert(dataArray[counter]);//Showing two values
counter++;
};
return dataArray;
}
这是我的其他js文件:
quizes = db.quizfun();
alert(quizes[0]);//working
alert(quizes[1]);//alert not showing anything
你能告诉我我做错了什么。谢谢你提前
答案 0 :(得分:1)
var counter = 0;
应该在while循环之外。像
function quizfun() {
var dataArray=new Array();
var quizes = db.execute('select * from Quiz');
var counter = 0;
while (quizes.isValidRow()) {
dataArray[counter] = quizes.fieldByName('Quiz_Text');
quizes.next();
alert(dataArray[counter]);//Showing two values
counter++;
};
return dataArray;
}