gpt4 book ai didi

javascript - 检查选定的单选按钮是否等于数组中存储的 "answer"

转载 作者:行者123 更新时间:2023-11-28 01:19:45 25 4
gpt4 key购买 nike

菜鸟开发人员,对任何非常规代码表示歉意。

我正在做一个简单的测验。一次显示一个问题,单选按钮上有 3 个可能的答案。答案“key”存储在名为 allQuestions 的数组中。

我正在寻找正确的方法来比较用户的答案(所选的单选按钮)是否等于正确的答案。

我更喜欢使用 JQuery 进行解释,但不反对 getElementByIdea 解决方案。哎呀,为什么不涵盖两者呢!你会在 JS 部分看到我尝试了两者。

最后,我应该在这两种方法中使用函数吗?或者一个独立的 If/Then 就足够了?您会看到我在 getElementByIdea 尝试中包含了一个。

JSFiddle:http://jsfiddle.net/Ever/fYA5Y/

HTML

<body>
<h2>JS and JQuery Quiz</h2>

<div class="intro">
Welcome! When the button at the bottom is clicked, the question and answers below will
progress to the next question and respective answer chioces. Good luck!
<br>
</div>

<br>

<div class="questions">Ready?</div>

<br>

<input type="radio" id="radio1" name="radios"><label id="r1"> Yes</label></br>
<input type="radio" id="radio2" name="radios"><label id="r2"> No</label></br>
<input type="radio" id="radio3" name="radios"><label id="r3"> Wat </label></br>

</br>

<button>Submit</button>
</body>

CSS

div.intro{
font-weight: bold;
width: 50%;
}

div.questions{
text-align: left;
font-size: 25px;
width: 500px;
height: 100px;
border: 1px solid black;
padding: 5px;
}

JS

$(document).ready(function(){ 
//store quetsions, answer options, and answer key
var allQuestions = {
question: ["Who is Prime Minister of the United Kingdom?", "Which of the \
following is not a breed of dog?", "What sound does a pig make?", "When presented dessert menu,
\
which item would Anne most likely order?"],
choices: [" David Cameron", " Gordon Brown", " Winston Churchill", " Retriever" , " Poodle", "
Tabby", " Moo", " Oink", " Meow", "Salted Caramel Ice Cream", "Lime and Ginger Sorbet", "Double
Chocolate Cake"],
answers: [1,1,1,1],
};

//global counters for each of the questions and answer choices
var q = 0;
var rb1= 0;
var rb2=1;
var rb3=2;
var ans=0;

//global counters to store the amount of correct and incorrect user answers
var userScore = {
correct: 0,
incorrect: 0,
}

//when button is clicked, update the quetstion to show the next question, and the
//radio buttons to show the next answer options.
$('button').click(function(){
//locate the current question and answer choices and set them to variables
var currentQuestion = (allQuestions.question[q]);
var currentRadio1 = (allQuestions.choices[rb1]);
var currentRadio2 = (allQuestions.choices[rb2]);
var currentRadio3 = (allQuestions.choices[rb3]);
var currentAnswer = (allQuestions.answers[ans]);
//update html to display the current question and answer choices
$('.questions').html(currentQuestion);
$('#r1').html(currentRadio1);
$('#r2').html(currentRadio2);
$('#r3').html(currentRadio3);
//progress the question and answer choice counters
q = q + 1;
rb1 = rb1 + 3;
rb2 = rb2 + 3;
rb3 = rb3 + 3;
ans = ans + 1;

//Using JQuery, compare the user's answer (the selected radio button) to the correct
//answer.
//Do this by having the code look at all the radio buttons. If the checked one
//is equal to the correct answer, increment "correct" in <userScore>.
//If incorrect, increment "incorrect" in <userScore>.

if $('input["radios"]:checked' == currentAnswer){
userScore.correct++;
}
else{
userScore.incorrect++;
}

//Alternative method: use getElementByID to do tally answers.
//function tallyAnswers() {
//if (document.getElementById("radios").checked == currentAnswer) {
//userScore.correct++;
//}
//else {
//userScore.incorrect++;
//}
//}

//when all questions have been done, alert the total correct and incorrect answers
//alert(userScore.correct)
});
});



//Psuedocode:
//Use a JS object to separately hold each group of: questions, choices and correct answers.
//Use a JS function so that when <button> is clicked, it:
//**removes the current text from the <.questions> DIV
//**clears the radio buttons
//**adds the next question's text from <allQuestions> to the <.questions> DIV
//**adds the next anwers the radio buttons
//Use a JS object to store each of the user's answers, which are determined by which
//radio button is selected when <button> is clicked.
//If user clicks <button> without first selecting a radio button, do not update the form, and
//do not store their answer. Instead, alert the user.
//On the final page, let the user know they are done. Tally and display the total
//amount of correct answers.

最佳答案

http://jsfiddle.net/fYA5Y/1/

我已经清理了你的代码,你的尝试很好,只是语法错误

if ($('input[name="radios"]:checked').val() == currentAnswer){
userScore.correct++;
} else{
userScore.incorrect++;
}
console.log(userScore);

如您所见,您应该使用 $('input[name='xxxx']) 按名称获取元素,还应该为输入标签设置值

<input type="radio" id="radio1" name="radios" value="1">

关于javascript - 检查选定的单选按钮是否等于数组中存储的 "answer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23378676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com