我是Javascript和Jquery的新手,目前我正在和一个队友一起开展一个琐事测验项目。使用预先编写的JQuery代码,我必须从队友创建的外部文本文件中检索数据,该文件以SQL格式编写,并将该数据作为测验的问题和答案传递。我想知道检索数据的最佳方法是什么。
这是Jquerycode的当前迭代,并且已被修剪以显示存在问题的数组,以及获取问题和答案的函数。
$(function () {
var questions = [{
question: "What is 2*5?",
choices: [2, 5, 10, 15, 20],
correctAnswer: 2
}, {
question: "What is 3*6?",
choices: [3, 6, 9, 12, 18],
correctAnswer: 4
},
}];
// Display initial question
displayNext();
// Creates and returns the div that contains the questions and
// the answer selections
function createQuestionElement(index) {
var qElement = $('<div></div>', {
id: 'question'
});
var header = $('<h2>Question ' + (index + 1) + ':</h2>');
qElement.append(header);
var question = $('<p>').append(questions[index].question);
qElement.append(question);
var radioButtons = createRadios(index);
qElement.append(radioButtons);
return qElement;
}
// Creates a list of the answer choices as radio inputs
function createRadios(index) {
var radioList = $('<ul>');
var item;
var input = '';
for (var i = 0; i < questions[index].choices.length; i++) {
item = $('<li>');
input = '<input type="radio" name="answer" value=' + i + ' />';
input += questions[index].choices[i];
item.append(input);
radioList.append(item);
}
return radioList;
}
})();
这是一个带有一个问题的文本文件,真实文件中有多个。
create table TriviaQuestion
{
questionNum int,
question VARCHAR(255),
option1 VARCHAR(255),
option2 VARCHAR(255),
option3 VARCHAR(255),
option4 VARCHAR(255),
option5 VARCHAR(255),
questionAnswer VARCHAR(100);
CONSTRAINT [PK_questionNum]
}
Insert into TriviaQuestion(questionNum,question,option1,option2,option3,option4,option5,questionAnswer)
Values('1','Which list contains words that are NOT names of shoe types:',
'A. Oxford, jelly, boat, clogs, stiletto, mary jane',
'B. Loafer, gladiator, wedge, mule, platform',
'C. Pump, moccasin, wingtip, sneaker, derby, monk',
'D. Chalupa, dogler, hamster, croonley, frankfurt',
'',
'D. Chalupa, dogler, hamster, croonley, frankfurt')
;