使用jquery和javascript进行测验
我正在用javascript和jquery做一个测试,但我完全不知道如何继续。现在我只创建了一个JSON来保存这个问题 迄今为止的Javascript文档:使用jquery和javascript进行测验,javascript,jquery,html,Javascript,Jquery,Html,我正在用javascript和jquery做一个测试,但我完全不知道如何继续。现在我只创建了一个JSON来保存这个问题 迄今为止的Javascript文档: // JavaScript Document (function(){ window.onload = init; function init(){ } var points = 0; var numberofquestions= 0; var correct= 0; v
// JavaScript Document
(function(){
window.onload = init;
function init(){
}
var points = 0;
var numberofquestions= 0;
var correct= 0;
var quiz= {question: [
{Question: "Question 1?",
answers: [
{answer: "Answer 1", correct_answer: 0},
{answer: "Answer 2", correct_answer: 1},
{answer: "Answer 3", correct_answer: 0}
]}
]};
})();
HTML
测验
测验
上
下一个
我没有要求任何人这样做,但我需要帮助来取得任何进展。我试着用谷歌搜索类似的东西,但它看起来太复杂了,我弄不清模式。
谢谢你我会给你一些想法:
- 你的提问结构是正确的,你的初始变量选择正确。这是一个好的开始。我又错过了一个变量“current_question”,您将其设置为0
- 然后是HTML部分。这似乎也是一个适当的结构来描绘问题
- 1) 在HTML中绘制一个问题
- 2) 当用户单击答案时,查看当前问题中的测验数组,并检查其是否正确
- 3) 如果正确,您将向point变量添加一些点李>
- 4) 您将执行当前问题=当前问题+1并返回步骤1)
var current_question_number=0;
var currentQuestion=null; // we will store the current question here for easy access
function start_quiz() {
current_question_number=0;
correct=0;
points=0;
paint_question();
}
// you will call this to paint the current question current_question_number
function paint_question() {
currentQuestion=quiz.question[current_question_number];
// paint the title
// paint inside a HTML by id: Standard javascript way
document.getElementById("title").innerText=currentQuestion.Question;
// or use jquery: $("#title").text(currentQuestion.Question);
// paint the answers
var answers=currentQuestion.answers;
for (var i=0; i<answers.length; i++) {
var answer=answers[i]; // current answer
// now you have to add <li>answer</li> to the <ul>
// this is your homework!
}
}
// you will call this when the user clicks on one answer, with the answer number
function receive_answer(chosen) {
if (currentQuestion.answers[chosen].correct_answer==1) {
// add the points
}
current_question++;
paint_question();
}
var当前问题编号=0;
var currentQuestion=null;//为了便于访问,我们将当前问题存储在这里
函数启动_测验(){
当前问题编号=0;
正确=0;
分值=0;
画画问题();
}
//您将调用此函数来绘制当前问题当前问题编号
函数paint_question(){
currentQuestion=测验。问题[当前问题编号];
//画标题
//按id在HTML内部绘制:标准javascript方式
document.getElementById(“title”).innerText=currentQuestion.Question;
//或者使用jquery:$(“#title”).text(currentQuestion.Question);
//画出答案
var answers=当前问题答案;
对于(var i=0;i我将给您一些想法:
- 你的提问结构是正确的,你的初始变量选择正确。这是一个很好的开始。我错过了另外一个变量“current_question”,你将其设置为0
- 然后是HTML部分,它似乎也是一个足够的结构来描述问题
现在,缺少的是第三部分:逻辑。您需要编写一个程序:
- 1) 在HTML中绘制一个问题
- 2) 当用户单击答案时,查看当前问题中的测验数组,并检查其是否正确
- 3) 如果正确,您将向point变量添加一些点
- 4) 您将执行当前问题=当前问题+1并返回步骤1)
这可以补充你的计划
var current_question_number=0;
var currentQuestion=null; // we will store the current question here for easy access
function start_quiz() {
current_question_number=0;
correct=0;
points=0;
paint_question();
}
// you will call this to paint the current question current_question_number
function paint_question() {
currentQuestion=quiz.question[current_question_number];
// paint the title
// paint inside a HTML by id: Standard javascript way
document.getElementById("title").innerText=currentQuestion.Question;
// or use jquery: $("#title").text(currentQuestion.Question);
// paint the answers
var answers=currentQuestion.answers;
for (var i=0; i<answers.length; i++) {
var answer=answers[i]; // current answer
// now you have to add <li>answer</li> to the <ul>
// this is your homework!
}
}
// you will call this when the user clicks on one answer, with the answer number
function receive_answer(chosen) {
if (currentQuestion.answers[chosen].correct_answer==1) {
// add the points
}
current_question++;
paint_question();
}
var当前问题编号=0;
var currentQuestion=null;//为了便于访问,我们将当前问题存储在这里
函数启动_测验(){
当前问题编号=0;
正确=0;
分值=0;
画画问题();
}
//您将调用此函数来绘制当前问题当前问题编号
函数paint_question(){
currentQuestion=测验。问题[当前问题编号];
//画标题
//按id在HTML内部绘制:标准javascript方式
document.getElementById(“title”).innerText=currentQuestion.Question;
//或者使用jquery:$(“#title”).text(currentQuestion.Question);
//画出答案
var answers=当前问题答案;
对于(var i=0;我不确定你想做什么…你能更具体一点吗?我正在尝试做一个测验。这是从JSON对象中获取问题。我不确定你想做什么…你能更具体一点吗?我正在尝试做一个测验。这是从JSON对象中获取问题。