- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用单选按钮制作一个简单的 Javascript 测验。每个问题和选项都应存储在一个数组中,并使用 getElementById()
动态替换问题。我一直在尝试为单选按钮分配值,以便我可以在最后显示分数。
我是 javascript 新手,这是我正在从事的第一个项目。
这是我到目前为止所拥有的:
var questionsArray = new Array;
questionsArray[0] = ["1. Who is the president of the US?"];
questionsArray[1] = ["2. What is the capital city of France?"];
questionsArray[2] = ["3. What is your favorite food?"];
var choicesArray = new Array();
choicesArray[0] = ["Lebron James", "Barack Obama", "George Bush"];
choicesArray[1] = ["Nairobi", "London", "Paris"];
choicesArray[2] = ["Pizza", "Sushi", "Pasta"];
var i = 0;
var theScore = 0;
function btnOnclick() {
var radElement = document.form1.options;
var showDiv = document.getElementById("questionsDiv");
showDiv.className = "showClass";
document.getElementById("next").value = "Next";
while(i < questionsArray.length && i < choicesArray.length) {
var currQues = questionsArray[i];
var option1 = choicesArray[i][0];
var option2 = choicesArray[i][1];
var option3 = choicesArray[i][2];
i++;
document.getElementById("question").innerHTML = currQues;
document.getElementById("choice1").innerHTML = option1;
document.getElementById("choice2").innerHTML = option2;
document.getElementById("choice3").innerHTML = option3;
return true
}
if(questionsArray[0] && radElement[1].checked == true) {
theScore += 10
} else {
theScore += 0
}
if(questionsArray[1] && radElement[2].checked == true) {
theScore += 10
} else {
theScore += 0
}
if(questionsArray[2] && radElement[1].checked == true) {
theScore += 10
} else {
theScore += 0
}
alert(theScore)
}
function bodyOnload() {
var hideDiv = document.getElementById("questionsDiv");
hideDiv.className = "hideClass";
document.getElementById("next").value = "Start Quiz"
}
.hideClass{
display: none;
}
.showClass{
display: block;
}
<body onload="bodyOnload()">
<h1>QUIZ</h1>
<div id="questionsDiv">
<form name="form1">
<p id="question" class="showQuestion"></p>
<tr>
<span id="choice1"></span>
<td>
<input type="radio" id="radio1" value="0" name="options" />
</td>
<td>
<span id="choice2"></span>
<input type="radio" id="radio2" value="0" name="options" />
</td>
<td>
<span id="choice3"></span>
<input type="radio" id="radio3" value="0" name="options" />
</td>
</tr>
</div>
<br />
<br />
<hr>
<tr>
<td>
<input type="button" id="next" value="Next" name="nextButton" onclick="btnOnclick()"/>
</td>
</tr>
</form>
</body>
最佳答案
考虑将您的答案构建为对象数组,如下所示:
choicesArray[2] = [{
text: "Pizza",
correct: true
}, {
text: "Pasta",
correct: false
}, {
text: "Sushi",
correct: false
}];
然后,如果选择了radio2
,则检查答案数组中的第二项以查看其是否正确,如下所示:
// use index 1 for the second choice in the list.
if(choicesArray[2][1].correct === true) {
//it's right!
} else {
//it's wrong...
}
在实现此功能之前,显然您还有很长的路要走,但这是我的第一个建议。
您可以更进一步(我愿意!),将问题和答案放在一起以便于引用:
var question = {
questionText: 'What is your favorite food?',
answers: [{
answerText: "Pizza",
correct: true
}, {
answerText: "Pasta",
correct: false
}, {
answerText: "Sushi",
correct: false
}]
};
然后您可以访问如下内容:
var currentQuestionIndex = 1; //or whatever it is
var questionText = questionsArray[currentQuestionIndex].questionText;
var answer1 = questionsArray[currentQuestionIndex].answers[0].answerText;
var answer1isCorrect = questionsArray[currentQuestionIndex].answers[0].correct;
var answer2 = questionsArray[currentQuestionIndex].answers[1].answerText;
var answer2isCorrect = questionsArray[currentQuestionIndex].answers[1].correct;
var answer3 = questionsArray[currentQuestionIndex].answers[2].answerText;
var answer3isCorrect = questionsArray[currentQuestionIndex].answers[2].correct;
此外,在向数组添加项目时不需要如此明确。它使您的代码变得脆弱。考虑使用push()
:
var questionsArray = [];
questionsArray.push({
questionText: 'What is your favorite food?',
answers: [{
answerText: "Pizza",
correct: true
}, {
answerText: "Pasta",
correct: false
}, {
answerText: "Sushi",
correct: false
}]
});
关于javascript - 使用单选按钮来计算测验的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31093396/
我知道这曾经是一个话题,但搜索后我找不到答案 - 也许我的问题太基本了。不过,我正在创建一个 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
我是一名优秀的程序员,十分优秀!