【发布时间】:2016-04-07 15:33:11
【问题描述】:
我正在尝试使用纯 javascript 来创建点击个性测验以进行单项作业。我有一堆 div 布局,以便它们应该出现。每个问题都有一个列表,其中包含 4 个(左右)与不同性格类型相关的答案。我打算保留一组对象来统计每个选定的答案,并在最后提供人格类型的细分。
我目前被困在功能上;
- 记录选中的数据并将其添加到answers数组中,
- 隐藏当前问题的div和
- 显示下一个问题 div
HTML:
<div class="question" id="q1" data-next="q2">
<h2>Question 1:</h2>
<p>Which of the following is your favourite movie? </p>
<ol class="button">
<li data-score="Ninja">Karate Kid</li>
<li data-score="Robot">Wall-E</li>
<li data-score="Pirate">Pirates of the Caribbean</li>
<li data-score="Zombie">Dawn of the Dead</li>
</ol>
</div>
<div class="question" id="q2" data-next="q3">
<h2>Question 2:</h2>
<p>A building is on fire and you hear a child's screaming for help from the third floor window. Do you: </p>
<ol class="button">
<li data-score="Ninja">Mysteriously disappear and re-appear with the children</li>
<li data-score="Robot">Run in and save the child on the second floor, because i'm made of metal and fire won't hurt me!</li>
<li data-score="Pirate">Dress up as a pirate and loot the surrounding neighbourhood, including the bank?</li>
<li data-score="Zombie">Eat all the brains. Nom nom uuuuggghhh.</li>
</ol>
</div>
JS:
// Create a listener for clicks on the 'start the quiz' button on the front page.
document.getElementById("beginquiz").addEventListener("click", startQuiz);
// When the button is clicked the 'intro' div is hidden and the first question div is displayed
function startQuiz () {
document.getElementById("intro").style.display = "none";
document.getElementById("q1").style.display = "block";
}
// Create an array object to store all the quiz answers. Each selected answer should increase the category score by 1. The highest score will be the personality 'type' in the results.
var answerData = [
{name: "Ninja" , score: 0},
{name: "Robot" , score: 0},
{name: "Pirate" , score: 0},
{name: "Zombie" , score: 0} ]
// Get all of the .buttons elements
var buttons = document.querySelectorAll(".button");
// Add an onclick event listener to every element with a class of .buttons
for (var i = 0 ; i < buttons.length ; i++) {
// When an element with .buttons is clicked, run the function called buttonClicked
buttons[i].onclick = buttonClicked;
}
// Define what buttonClicked does
function buttonClicked() {
// Get the current element's data-score value
var selectedType = this.dataset.score;
// Increase the selected answer's 'type' by 1
answerData["selectedType"].score++;
// Hide the current question div
this.parentElement.style.display = "none";
// Work out what the next question div is
var nextQuestion = this.parentElement.dataset.next;
// Display the next question element
document.getElementById(nextQuestion).style.display = "block";
}
摆弄我到目前为止所做的事情https://jsfiddle.net/funkefiddle/e1za0gtr/1/
出于某种原因,我认为 data-score 是一个在可点击答案元素之间建立联系并实际跟踪它的好地方。但是,显然我的代码实际上并没有工作。 Firefox 控制台显示“this.dataset.score is undefined”。
var selectedType = this.dataset.score.value;
answerData["selectedType"].score++;
请停下来。
另外 - 我不知道显示系列中下一个元素的代码是否可以工作,因为我的错误检查还没有做到那么远。我只是写了我的大脑建议可能有用的东西。
编辑:去掉 .value 因为我不知道为什么我一开始就有它。 还更改了最后一行以使 nextQuestion 成为变量而不是字符串。问题现在显示/隐藏在进程中(当我注释掉 answerData 行时。
我想这意味着我被困在我想增加所选答案类型的数组值的行上。
answerData[selectedType].score++ ;
【问题讨论】:
标签: javascript html