knockoutjs - 做一个测验
knockoutjs 文件:
$(function () {
$('#info').hide();
QuizViewModel();
ko.applyBindings(new QuizViewModel());
$('#restart').click(function () {
location.reload();
});
});
function QuizViewModel() {
var self = this;
self.previous = ko.observableArray([]);
self.questions = ko.observableArray([]);
self.current = ko.observable();
self.number = ko.observable(0);
self.previousNumbers = ko.observableArray([]);
self.selectedAnswers = ko.observableArray();
self.correctAnswers = ko.observableArray([]);
self.loadQuestions = function () {
$('#questionsAndAnswers').fadeOut('fast');
$.getJSON('./json/quiz.json', function (data) {
$.each(data, function (i, q) {
self.questions.push(q);
if (q.answers.tf == "true") {
self.correctAnswers.push(q.answers.question);
}
else {
//
}
});
});
$('#questions').fadeIn('fast');
}
self.getQuestion = function (number) {
$.getJSON('./json/quiz.json', function (data) {
$.each(data, function (i, q) {
if (number == i) {
self.current(q);
}
});
});
}
self.nextQuestion = function (selectedAnswer) {
if (self.previousNumbers().length == 15) {
$('#questionsAndAnswers').fadeIn('fast');
$('#questions').fadeOut('fast');
} else {
var random = Math.floor(Math.random() * 15)
if (self.previousNumbers.indexOf(random) == -1) {
if (self.previousNumbers().length > 0) {
self.current().selectedAnswers = self.selectedAnswers();
//alert(self.current().selectedAnswers[0] + " " + self.current().selectedAnswers[1]);
if ($.inArray(self.current().selectedAnswers[0], this.correctAnswers) > -1) {
$('#infoCorrect').show();
}
self.previous.push(self.current());
self.selectedAnswers.removeAll();
}
self.previousNumbers.push(random);
self.getQuestion(random);
var previousNumber = self.number();
self.number(previousNumber + 1);
} else {
self.nextQuestion();
}
}
}
$('#questionsAndAnswers').fadeOut('fast');
self.nextQuestion();
}
我的 json 文件的第一部分:tf = true 或 false(只是为了给它一个名称)
[
{"id" : 1,
"question": "Welke stad is de hoofdstad van Brazili\u00eb?",
"answers" : [{"answer":"Rio de Janeiro", "tf":"false"},
{"answer":"Brasilia", "tf":"true"},
{"answer":"Sa\u00F5 Paulo", "tf":"false"}],
"info" : "De hoofdstad van Brazili\u00eb is Brasilia en niet Rio de Janeiro of Sa\u00F5 Paulo zoals de meesten denken. Tot 1960 was Rio de Janeiro inderdaad de hoofdstad, maar vanaf dan nam de nieuwe stad Brasilia deze functie over. Niettemin zijn Rio de Janeiro en Sa\u00F5 Paulo zeer belangrijke steden voor het land met respectievelijk 11 en 6 miljoen inwoners."
}, ...
html5 页面:
<div id ="questions" data-bind="with: current">
<h1 id="title">Quiz rond het thema: Brazilië - Sisal</h1>
<p class="question" data-bind="text: question"></p>
<div class="answers" data-bind="foreach: answers">
<p data-bind="with: $data">
<input id="checkboxes"type="checkbox" data-bind="checked: $root.selectedAnswers, value: answer"/>
<span class="answer" data-bind="text: answer"></span>
</p>
</div>
<p id="info" class = "answers" data-bind="text: info"></p>
<img id="img1" class="buttons" src="img/next.png" title="Volgende vraag" data-bind="click: $root.nextQuestion"/>
</div>
</section>
<div id ="questionsAndAnswers">
<div>
<div data-bind="foreach: previous">
<p class="question" data-bind="text: question"></p>
<div data-bind="foreach: selectedAnswers">
<span data-bind="text: $data"></span>
</div>
<div data-bind="foreach: answers">
<p data-bind="with: $data">
<input type="checkbox" data-bind="value: answer, checked: tf=='true'" disabled="true"/>
<span class="answer" data-bind="text: answer"> </span><span data-bind="checked: $parent.selectedAnswers"></span>
</p>
</div>
<div>
</div>
</div>
</div>
<img id="restart" class="buttons" src="img/start.png" title="Herstart de quiz" />
</div>
有人可以告诉我如何使用 json 文件中的正确答案检查所选答案..然后显示带有 id="info" 的 p 标签?
我现在使用一个数组来检查这个(正确答案)
knockoutjs file:
$(function () {
$('#info').hide();
QuizViewModel();
ko.applyBindings(new QuizViewModel());
$('#restart').click(function () {
location.reload();
});
});
function QuizViewModel() {
var self = this;
self.previous = ko.observableArray([]);
self.questions = ko.observableArray([]);
self.current = ko.observable();
self.number = ko.observable(0);
self.previousNumbers = ko.observableArray([]);
self.selectedAnswers = ko.observableArray();
self.correctAnswers = ko.observableArray([]);
self.loadQuestions = function () {
$('#questionsAndAnswers').fadeOut('fast');
$.getJSON('./json/quiz.json', function (data) {
$.each(data, function (i, q) {
self.questions.push(q);
if (q.answers.tf == "true") {
self.correctAnswers.push(q.answers.question);
}
else {
//
}
});
});
$('#questions').fadeIn('fast');
}
self.getQuestion = function (number) {
$.getJSON('./json/quiz.json', function (data) {
$.each(data, function (i, q) {
if (number == i) {
self.current(q);
}
});
});
}
self.nextQuestion = function (selectedAnswer) {
if (self.previousNumbers().length == 15) {
$('#questionsAndAnswers').fadeIn('fast');
$('#questions').fadeOut('fast');
} else {
var random = Math.floor(Math.random() * 15)
if (self.previousNumbers.indexOf(random) == -1) {
if (self.previousNumbers().length > 0) {
self.current().selectedAnswers = self.selectedAnswers();
//alert(self.current().selectedAnswers[0] + " " + self.current().selectedAnswers[1]);
if ($.inArray(self.current().selectedAnswers[0], this.correctAnswers) > -1) {
$('#infoCorrect').show();
}
self.previous.push(self.current());
self.selectedAnswers.removeAll();
}
self.previousNumbers.push(random);
self.getQuestion(random);
var previousNumber = self.number();
self.number(previousNumber + 1);
} else {
self.nextQuestion();
}
}
}
$('#questionsAndAnswers').fadeOut('fast');
self.nextQuestion();
}
first part of my json file: tf = true or false (just to give it a name)
[
{"id" : 1,
"question": "Welke stad is de hoofdstad van Brazili\u00eb?",
"answers" : [{"answer":"Rio de Janeiro", "tf":"false"},
{"answer":"Brasilia", "tf":"true"},
{"answer":"Sa\u00F5 Paulo", "tf":"false"}],
"info" : "De hoofdstad van Brazili\u00eb is Brasilia en niet Rio de Janeiro of Sa\u00F5 Paulo zoals de meesten denken. Tot 1960 was Rio de Janeiro inderdaad de hoofdstad, maar vanaf dan nam de nieuwe stad Brasilia deze functie over. Niettemin zijn Rio de Janeiro en Sa\u00F5 Paulo zeer belangrijke steden voor het land met respectievelijk 11 en 6 miljoen inwoners."
}, ...
html5 page:
<div id ="questions" data-bind="with: current">
<h1 id="title">Quiz rond het thema: Brazilië - Sisal</h1>
<p class="question" data-bind="text: question"></p>
<div class="answers" data-bind="foreach: answers">
<p data-bind="with: $data">
<input id="checkboxes"type="checkbox" data-bind="checked: $root.selectedAnswers, value: answer"/>
<span class="answer" data-bind="text: answer"></span>
</p>
</div>
<p id="info" class = "answers" data-bind="text: info"></p>
<img id="img1" class="buttons" src="img/next.png" title="Volgende vraag" data-bind="click: $root.nextQuestion"/>
</div>
</section>
<div id ="questionsAndAnswers">
<div>
<div data-bind="foreach: previous">
<p class="question" data-bind="text: question"></p>
<div data-bind="foreach: selectedAnswers">
<span data-bind="text: $data"></span>
</div>
<div data-bind="foreach: answers">
<p data-bind="with: $data">
<input type="checkbox" data-bind="value: answer, checked: tf=='true'" disabled="true"/>
<span class="answer" data-bind="text: answer"> </span><span data-bind="checked: $parent.selectedAnswers"></span>
</p>
</div>
<div>
</div>
</div>
</div>
<img id="restart" class="buttons" src="img/start.png" title="Herstart de quiz" />
</div>
Can someone tell me how to check the selected answer with the correct answer in the json file.. And then show the p tag with id="info" ?
i'm using an array now to check this (correctAnswers)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我根据我的需要简化了您的代码。这是一个工作示例 http://jsfiddle.net/gurkavcu/wJhqB/
摘要:
我使用了淘汰赛用于显示结果和模拟 ajax 事件的映射插件:
映射插件文档:http://knockoutjs.com/documentation/plugins-mapping.html
映射插件来源: https://raw .github.com/SteveSanderson/knockout.mapping/master/build/output/knockout.mapping-latest.js
I simplified your code for my needs. Here is a working example http://jsfiddle.net/gurkavcu/wJhqB/
Summary :
I used knockout mapping plugin for displaying results and simulating ajax events :
Mapping plugin Doc : http://knockoutjs.com/documentation/plugins-mapping.html
Mapping plugin Source : https://raw.github.com/SteveSanderson/knockout.mapping/master/build/output/knockout.mapping-latest.js