亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當str設置為opt2時,為什么分數會評估分數+1

當str設置為opt2時,為什么分數會評估分數+1

飲歌長嘯 2021-04-26 20:17:44
我有一個函數,可以根據選擇的選項來計算調查中的分數,但是我總是得到與第一個if條件相同的值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);
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

如果我對您的理解很好,那么您的問題可能是由于以下另一個原因:

  1. 您不會在<select>元素上觸發更改事件。

  2. answer的價值,您可能需要使用.selectedIndex獲得的回答值。

看看 onchange事件,.selectedIndex

var questions = [{

  opt1: "Answer 1",

  opt2: "Answer 2",

  opt3: "Answer 3"

}];


// score1, currentQuestion variables defined as example

var currentQuestion = 0;

var score1 = 1;


var sel = document.getElementById("sel");


sel.addEventListener("change", function() {

  var answer = this.options[this.selectedIndex].value;

  if (questions[currentQuestion].opt1 == answer) {

    score1 += 1;

  } else if (questions[currentQuestion].opt2 == answer) {

    score1 += 2;

  } else {

    score1 += 3;

  }

  console.log("score1 = " + score1)

});

<div id="q"></div>

<select id="sel">

  <option value="Answer 1">Answer 1</option>

  <option value="Answer 2">Answer 2</option>

  <option value="Answer 3">Answer 3</option>

</select>


查看完整回答
反對 回復 2021-05-06
  • 1 回答
  • 0 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號