Note that there are some explanatory texts on larger screens.

plurals
  1. POknockoutjs - making a quiz
    primarykey
    data
    text
    <p>knockoutjs file:</p> <pre><code> $(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 &gt; 0) { self.current().selectedAnswers = self.selectedAnswers(); //alert(self.current().selectedAnswers[0] + " " + self.current().selectedAnswers[1]); if ($.inArray(self.current().selectedAnswers[0], this.correctAnswers) &gt; -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(); } </code></pre> <p>first part of my json file: tf = true or false (just to give it a name)</p> <pre><code> [ {"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." }, ... </code></pre> <p>html5 page:</p> <pre><code> &lt;div id ="questions" data-bind="with: current"&gt; &lt;h1 id="title"&gt;Quiz rond het thema: Brazili&amp;euml; - Sisal&lt;/h1&gt; &lt;p class="question" data-bind="text: question"&gt;&lt;/p&gt; &lt;div class="answers" data-bind="foreach: answers"&gt; &lt;p data-bind="with: $data"&gt; &lt;input id="checkboxes"type="checkbox" data-bind="checked: $root.selectedAnswers, value: answer"/&gt; &lt;span class="answer" data-bind="text: answer"&gt;&lt;/span&gt; &lt;/p&gt; &lt;/div&gt; &lt;p id="info" class = "answers" data-bind="text: info"&gt;&lt;/p&gt; &lt;img id="img1" class="buttons" src="img/next.png" title="Volgende vraag" data-bind="click: $root.nextQuestion"/&gt; &lt;/div&gt; &lt;/section&gt; &lt;div id ="questionsAndAnswers"&gt; &lt;div&gt; &lt;div data-bind="foreach: previous"&gt; &lt;p class="question" data-bind="text: question"&gt;&lt;/p&gt; &lt;div data-bind="foreach: selectedAnswers"&gt; &lt;span data-bind="text: $data"&gt;&lt;/span&gt; &lt;/div&gt; &lt;div data-bind="foreach: answers"&gt; &lt;p data-bind="with: $data"&gt; &lt;input type="checkbox" data-bind="value: answer, checked: tf=='true'" disabled="true"/&gt; &lt;span class="answer" data-bind="text: answer"&gt; &lt;/span&gt;&lt;span data-bind="checked: $parent.selectedAnswers"&gt;&lt;/span&gt; &lt;/p&gt; &lt;/div&gt; &lt;div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;img id="restart" class="buttons" src="img/start.png" title="Herstart de quiz" /&gt; &lt;/div&gt; </code></pre> <p>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" ?</p> <p>i'm using an array now to check this (correctAnswers)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload