- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 http://javascriptissexy.com/how-to-learn-javascript-properly/ 上做“正确学习 JavaScript”类(class).
我花了很长时间,但我终于想出了如何进入下一个问题,但选择没有改变。
但是,当我对“questionIndex”进行硬编码时,问题和选择工作正常。
无论如何,这是我的代码(我知道它有点乱,我是初学者):
http://jsfiddle.net/utfwae8d/1/
HTML:
<div id="container">
<div id="quiz"></div>
<div id="choices"></div>
<input type="button" value="Next">
</div>
JavaScript:
var allQuestions = [{
question: "Who is the best in the world?",
choices: ["CM Punk", "John Cena", "Daniel Bryan", "Roman Reigns"],
correctAnswer: 0
},
{
question: "Who is the current WWE World Champion?",
choices: ["John Cena", "Brock Lesnar", "Triple H"],
correctAnswer: 1
},
{
question: "Where is Toronto located?",
choices: ["Ontario", "California", "Georgia", "Texas"],
correctAnswer: 0
},
{
question: "What is the largest California city?",
choices: ["Los Angeles", "San Fransico", "San Deigo", "Anahiem"],
correctAnswer: 0
}];
var quiz = document.getElementById('quiz');
var choicesContainer = document.getElementById('choices');
var nextButton = document.querySelector('[type=button]');
var correctAnswers = 0;
var questionIndex = 0;
function showQuiz() {
var currentQuestion = allQuestions[questionIndex].question;
quiz.textContent = currentQuestion;
var choicesNum = allQuestions[questionIndex].choices.length;
var correctAnswer = allQuestions[questionIndex].correctAnswer;
var choices;
for (var i = 0; i < choicesNum; i++) {
choices = allQuestions[questionIndex].choices[i];
choicesHTML = "<input type='radio' name='choice'>" + choices + "</br>";
choicesContainer.innerHTML += choicesHTML;
}
nextButton.addEventListener('click', function () {
questionIndex++;
quiz.textContent = allQuestions[questionIndex].question;
});
}
showQuiz();
最佳答案
按钮的点击处理程序不会更新答案,它只会更新问题。
我将代码分成两个函数:一个显示测验,一个显示答案。
var quiz = document.getElementById('quiz');
var choicesContainer = document.getElementById('choices');
var nextButton = document.querySelector('[type=button]');
var questionIndex = 0;
function showAnswers() {
choicesContainer.innerHTML = "";
var choicesNum = allQuestions[questionIndex].choices.length;
for (var i = 0; i < choicesNum; i++) {
var choice = allQuestions[questionIndex].choices[i];
choicesHTML = "<input type='radio' name='choice'>" + choice + "</br>";
choicesContainer.innerHTML += choicesHTML;
}
}
function showQuiz() {
var currentQuestion = allQuestions[questionIndex].question;
quiz.textContent = currentQuestion;
}
showQuiz();
showAnswers();
nextButton.addEventListener('click', function () {
questionIndex++;
showQuiz();
showAnswers();
});
关于javascript - 无法让我的测验应用程序工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832311/
我知道这曾经是一个话题,但搜索后我找不到答案 - 也许我的问题太基本了。不过,我正在创建一个 html 测验 - 5 页,分数从一页到下一页,然后在最后评分。 这是我正在尝试使用的代码,但它根本不起作
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在做一个简单的 php 问答游戏。我将问题和答案存储在数据库中。我的 tblQuiz 的表结构是这样的: _id, question, answer1, answer2, answer3, cor
我在考试中遇到了这个问题。 以下不正确 javascript 代码的输出是什么?错误是什么? num=0; if (num > 10); { num = num + 10; } alert(n
我在 JavaScript 中验证 4 个问题多项选择测验时似乎遇到了一些问题。现在,如果没有选择任何答案,则会弹出警报,但即使只选择了一个答案,警报也不会弹出。我正在将我的函数作为提交按钮的 onc
我正在尝试使用 JavaScript 构建一个问答游戏。 测验答案存储在变量中,如下所示: var correctAnswer = $('#createCorrectAnswer').val(); 然
下面我做了一个小测验,有 4 个问题,问题 2,3 和 4 被 CSS 隐藏了,但是你可以尝试从 CSS 中删除样式 (".pytja2, .pytja3, .pytja4 { display: no
private void startGame() { System.out.println("Exercises must be completed as quickly as pos
这听起来像是我之前问过的问题,但它只得到了部分回答。所以我想从我提出的问题开始。然后,一旦到达最后一个问题(#5),它就会返回到第一个问题(#1),当到达起始问题时,它将停止。我尝试在以下代码中执行此
愚蠢的问题,但我的教授不回复电子邮件,所以我在这里问。我现在正在复习期末考试,并且正在复习期中考试来学习。现在,这真的很愚蠢,因为我两个月前就答对了这个问题。但对于我的一生来说,现在回想起来,我无法弄
我无法编写一个循环来计算我为我的 wordpress 网站创建的测验的分数。 测验样式是“将 A 列中的项目与 B 列中的项目进行匹配”。 B 列项目使用选择元素来匹配 A 列中的相应项目。 我能想到
为什么选择不正确单选选项时,if语句中的else仍然出现? else 部分只应在未选择单选按钮时出现。真正的结果是下一个问题和可能的答案出现。这是 jsfiddle .我做错了什么? function
我正在尝试为西类牙语类(class)创建测验。我对 JavaScript 经验不多,但对 html 和 CSS 相当精通。我有一个问题,然后是三个带答案的单选按钮。有两个错误答案和一个正确答案。我总共
我正在编写一个允许用户设置个人测验的程序(如记事卡式系统)。参加测验时,用户会在每个问题上计时。虽然答案不会是多项选择,但它们将由用户输入。 有没有一种方法可以让我充分接受那些不是他们第一次设置测验时
我正在尝试根据找到的教程创建一个简单的测验。 http://css-tricks.com/building-a-simple-quiz/ 不幸的是,这让我无法自拔,答案可能非常简单。 我让这个工作完美
最近我遇到了以下测验。想象一下我们有这张 table +--------+ | colors | +--------+ | red | | black | | white | | green
这里的 W3Schools 网站上发布了一个 jQuery 测验... http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery 问题#1
我正在写一个简单的在线测验。有四个单选按钮作为选择。最初,脚本仅填充第一个问题的选项。之后,用户应该选择下一个并单击按钮。如果答案与正确的匹配,则总数递增。否则,将加载下一个问题。提交第 10 个问题
我不是 Haskell 程序员,但我对以下问题感到好奇。 非正式函数规范: 令 MapProduct 为一个函数,它采用名为 F 的函数和多个列表。它返回一个列表,其中包含使用每个可能组合中每个列表中
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!