Javascript 为什么分数评估分数+;1当str设置为opt2时
我有一个函数,根据选择的选项计算调查中的分数,但我总是得到与第一个if条件相同的值Javascript 为什么分数评估分数+;1当str设置为opt2时,javascript,Javascript,我有一个函数,根据选择的选项计算调查中的分数,但我总是得到与第一个if条件相同的值 var questions = [{ "question": "L'impact est ", option1: "faible", option2: "moyen", option3: "haut", }, var currentQu
var questions = [{
"question": "L'impact est ",
option1: "faible",
option2: "moyen",
option3: "haut",
},
var currentQuestion = 0;
var score1 = 0;
var totQuestions = questions.length;
var number = 0;
var progressText = document.getElementById("progressText");
var container = document.getElementById('quizContainer');
var questionEl = document.getElementById('question');
var opt1 = document.getElementById('opt1');
var opt2 = document.getElementById('opt2');
var opt3 = document.getElementById('opt3');
var nextButton = document.getElementById('nextButton');
var resultCont = document.getElementById('result');
function loadQuestion(questionIndex) {
var q = questions[questionIndex];
questionEl.textContent = (questionIndex + 1) + '. ' + q.question;
opt1.textContent = q.option1;
opt2.textContent = q.option2;
opt3.textContent = q.option3;
progressText.innerText = questionIndex + 1 + ' de ' + totQuestions + '
questions ';
};
function loadNextQuestion() {
var selectedOption =
document.querySelector('input[type=radio]:checked');
if (!selectedOption) {
alert('veuillez sélectionner votre réponse');
return;
}
var answer =selectedOption.Value;
if (questions[currentQuestion].opt1 == answer) {
score1 += 1;
} else if (questions[currentQuestion].opt2 == answer) {
score1 += 2;
} else {
score1 += 3
}
selectedOption.checked = false;
currentQuestion++;
progressText.textContent = questionEl / totQuestions
if (currentQuestion == totQuestions - 1) {
nextButton.textContent = 'Finish';
}
if (currentQuestion == totQuestions) {
container.style.display = 'none';
resultCont.style.display = '';
resultCont.textContent = 'le résultat est ' + ' ' + score1 + ' '
+answer+' '+ option1 ;
return;
}
loadQuestion(currentQuestion);
};
loadQuestion(currentQuestion);
如果我很了解你,你的问题可能是因为以下一个原因:
元素上触发更改事件答案
的值,您可能需要使用.selectedIndex
来获取答案值var问题=[{
opt1:“回答1”,
opt2:“回答2”,
opt3:“回答3”
}];
//分数1,定义为示例的currentQuestion变量
var-currentQuestion=0;
var评分1=1;
var sel=document.getElementById(“sel”);
sel.addEventListener(“更改”,函数(){
var answer=this.options[this.selectedIndex].value;
如果(问题[currentQuestion].opt1==答案){
分数1+=1;
}else if(问题[currentQuestion].opt2==答案){
得分1+=2;
}否则{
得分1+=3;
}
console.log(“score1=“+score1”)
});代码>
答复1
答复2
答复3
如果我很了解你,你的问题可能是因为以下一个原因:
您不会在
元素上触发更改事件
如果要查看答案
的值,您可能需要使用.selectedIndex
来获取答案值
看看
,
var问题=[{
opt1:“回答1”,
opt2:“回答2”,
opt3:“回答3”
}];
//分数1,定义为示例的currentQuestion变量
var-currentQuestion=0;
var评分1=1;
var sel=document.getElementById(“sel”);
sel.addEventListener(“更改”,函数(){
var answer=this.options[this.selectedIndex].value;
如果(问题[currentQuestion].opt1==答案){
分数1+=1;
}else if(问题[currentQuestion].opt2==答案){
得分1+=2;
}否则{
得分1+=3;
}
console.log(“score1=“+score1”)
});代码>
答复1
答复2
答复3
问题的内容是什么?(当我们在这里时,answer
的值是多少?);var answer=ans.options[ans.selectedIndex].value代码>问题的内容是什么?(当我们在这里时,answer
的值是多少?);var answer=ans.options[ans.selectedIndex].value代码>