【问题标题】:my if statement doesn't work when checking radio input检查无线电输入时,我的 if 语句不起作用
【发布时间】:2020-11-11 06:45:55
【问题描述】:

我正在尝试验证无线电输入以检查其答案是否正确。
但它跳过了if (answer == allQuestions[q].correctAnswer)

这一行

这里是完整代码https://jsfiddle.net/alcatel/sopfmevh/1/

for (let k = 0; k < answers.length; k++) {
  if (answers[k].checked) {
    answer = answers[k].value;
  }
}

// IT SKIPS THIS LINE !!
if (answer == allQuestions[q].correctAnswer) { // IT SKIPS THIS LINE!!
  alert("correct");
}

q++;
    
populate();

【问题讨论】:

  • 请将您的整个代码粘贴到问题中。避免链接。
  • 实际上我在您的 HTML 中没有看到任何名为“answers”的元素
  • radio.setAttribute("name", "answers");
  • 简单调试会告诉你为什么.....console.log(answer, allQuestions[q].correctAnswer, answer == allQuestions[q].correctAnswer);
  • 这通常是生成器函数的用例...

标签: javascript for-loop if-statement radio-button


【解决方案1】:

我认为你的 If 语句应该是这样的

if(answer == allQuestions[q].choices[allQuestions[q].correctAnswer]) {
        alert("correct");
    }

编辑了这个,刚刚尝试并为我工作

【讨论】:

  • 你原来的 IF 语句 allQuestions[q].correctAnswer 只是给你正确答案的索引,因为这是你存储在你的对象数组中的,所以你需要使用那个索引并将它放回去进入同一个数组,所以如果correctAnswerIndex = allQuestions[q].correctAnswer 那么allQuestions[q][correctAnswerIndex] 会给你正确的答案
  • 我认为存储数据的更好方法是将correctAnswer: 1替换为correctAnswer: Finland,这样您就可以做原来的事情
  • Ben - 您正在回答 Matias 的问题“为什么我的代码不起作用?”在评论中——why 应该是你的 answer 的一部分...这就是你所做的,这就是为什么它不起作用,这里是如何正确地做到这一点,代码解决 OP 问题的示例。
【解决方案2】:

您也可以使用数组中元素的索引进行比较。

if(allQuestions[q].choices.indexOf(answer) == allQuestions[q].correctAnswer) {
    alert("correct");
}

【讨论】:

    【解决方案3】:

    因为我做到了……

    const allQuestions = 
            [ { question     : 'Where is Helsinki?'
              , choices      : [ 'Sweden', 'Finland', 'Us' ] 
              , correctAnswer: 1
              } 
            , { question     : 'Where is Stockholm?'
              , choices      : [ 'Norway', 'Iceland', 'Sweden' ] 
              , correctAnswer: 2
              } 
            , { question     : 'Where is Köpenhamn?'
              , choices      : [ 'Denmark', 'Sweden', 'Norway' ] 
              , correctAnswer: 0
              } 
            ] 
    function* qList(arr) { for (let q of arr) yield q }
    
    const libQuestion  = document.querySelector('h1')
      ,   formReplies  = document.getElementById('list')
      ,   btNext       = document.querySelector('button')
      ,   DomParser    = new DOMParser()
      ,   qListRead    = qList( allQuestions )
      ,   score        = { correct: 0, count:0 }
      ;
    var currentAnswer = ''
      ;
    function setNewQuestion()
      {
      formReplies.innerHTML = ''
    
      let newQuestion = qListRead.next()
      if (!newQuestion.done)
        {
        libQuestion.textContent = newQuestion.value.question
        currentAnswer           = newQuestion.value.correctAnswer
        ++score.count
        newQuestion.value.choices.forEach((choice,indx)=>
          {
          let line = `<label><input type="radio" name="answer" value="${indx}">${ choice}</label>`
            , inLn = (DomParser.parseFromString( line, 'text/html')).body.firstChild
            ;
          formReplies.appendChild(inLn)
          })
        }
      else
        {
        libQuestion.textContent = ` Score = ${score.correct} / ${score.count}`
        btNext.disabled         = true
        }
      }
    setNewQuestion()
    btNext.onclick=()=>
      {
      if (formReplies.answer.value)
        {
        score.correct += ( currentAnswer == formReplies.answer.value )
        setNewQuestion()
        }
      }
    <h1></h1>
    <p></p>
    <form id="list"></form>
    <br>
    <button>next</button>

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 2016-02-17
      • 2020-03-04
      • 1970-01-01
      • 2021-09-23
      • 2013-08-16
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多