-1
https://codepen.io/abhilashn/pen/BRepQz如何定位到下一個對象在JavaScript
// JavaScript的文檔
var quiz = { "JS" : [
{
"id" : 1,
"question" : "Inside which HTML element do we put the JavaScript?",
"options" : [
{"a": "<script>", "b":"<javascript>", "c":"<scripting>", "d":"<js>"}
],
"answer":"<script>",
},
{
"id" : 2,
"question" : "What is the correct JavaScript syntax to change the content of the HTML element below.",
"options" : [
{"a": "document.getElementById('demo').innerHTML = 'Hello World!';",
"b":"document.getElementByName('p').innerHTML = 'Hello World!';",
"c":"document.getElement('p').innerHTML = 'Hello World!';",
"d":"#demo.innerHTML = 'Hello World!';"}
],
"answer":"a",
}
]
}
var score = 0;
var qno = 1;
var currentque = 0;
var totalque = quiz.JS.length;
displayQuiz = function(cque) {
currentque = cque;
$("#qid").html(this.qno);
//console.log(quiz.JS[currentque].options[0]);
$("#question").html(quiz.JS[currentque].question);
for (var key in quiz.JS[currentque].options[0]) {
if (quiz.JS[currentque].options[0].hasOwnProperty(key)) {
console.log(key + " -> " + quiz.JS[currentque].options[0][key]);
$("#question-options").append(
"<div class='form-check option-block'>" +
"<label class='form-check-label'>" +
"<input type='radio' class='form-check-input' name='option' id='q"+key+"' value='" + quiz.JS[currentque].options[0][key] + "'>" +
quiz.JS[currentque].options[0][key] +
"</label>"
);
}
}
}
checkAnswer = function(option) {
var answer = quiz.JS[currentque].answer;
option = option.replace(/\</g,"<") //for <
option = option.replace(/\>/g,">") //for >
if(option == quiz.JS[currentque].answer) {
score = score + 1;
}
}
changeQuestion = function(cque) {
currentque = currentque + cque;
displayQuiz(currentque);
}
$(document).ready(function() {
displayQuiz(0);
});
$('input[type=radio][name=option]').change(function() {
if (this.checked) {
checkAnswer(value);
}
});
$('#next').click(function() {
changeQuestion(1);
});
而你的問題是什麼? –
當我點擊下一步它應該顯示下一個問題 –