0

由于这两行,我不断收到此错误:

    document.getElementById('button').innerHTML = '<p><button 
    onClick = "MultiAnswer('+ questions[output] + ',' + answer[output] 
    +');">Submit</button></p>';

而且我无法弄清楚我错过了什么。

编辑:这是周围的代码(请原谅混乱)包含使用 switch 语句来确定所需数组的输入的方法,从那里将其放入 DisplayQuestion 的参数中,然后将其从所需的行为传递给下面的函数:

function MultiQuest(questions, choices, answer){
    var output = Math.floor(Math.random() * (questions.length));
    var choicesOut = [];

    document.getElementById('question').innerHTML = '<p id = "Q1">' + questions[output] + '<p><br>';

    for(var k = 0;k < choices[output].length; k++ ){
        choicesOut.push('<p><input id = "choice'+[k]+'" type = "radio" name = "option" value="'+choices[output][k]+'">' + choices[output][k] + '<p>');    
    }
    document.getElementById('answers').innerHTML = choicesOut.join("");
    document.getElementById('button').innerHTML = '<p><button onClick = "MultiAnswer('+ questions[output] + ',' + answer[output] +');">Submit</button></p>';
    document.getElementById('score').innerHTML = '<p>' + score + '<p>';
}

function MultiAnswer(questions, answer, pageType){

     var currentQuestion = document.getElementById('Q1').textContent;
     var number = multiQuestions(currentQuestion, questions);
     var correctAnswer = answer[number];
     var givenAnswer;

     var options = document.getElementsByName('option');
     var i
     for(i = 0; i < options.length; i++){
        if(options[i].checked){
            givenAnswer = options[i].value;
        }
     }

    if(givenAnswer == correctAnswer){
        alert("Right Answer!");
        score++;
    } else {
        alert("Wrong Answer!");
        score = 0;
    }
    i = 0;
    DisplayQuestion(pageType);
}

 function multiQuestions(currentQuestion, whichArray){
    for(var i = 0; i < multiquestions.length; i++){
        if(currentQuestion == whichArray[i]){
           return i;
        }
   }
   return null;
 }
4

2 回答 2

1

你不能有这样的函数调用:

MultiAnswer('+ questions[output] + ',' + answer[output] 
+')

您需要在单独的变量中评估参数,然后将其传递给函数。

于 2017-02-22T19:21:19.273 回答
0

因此,在您对 multiAnswer 的 onClick 调用中,您已将 3 个输入包含在引号中。在引用您的 multiAnswer 函数后,您确实拥有了您正在寻找的 3 个输入。这些输入的末端还有 + 号。您不需要在函数调用中连接括号。

我希望这有帮助! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

onClick = "MultiAnswer(questions[output] + ',' + answer[output] 
  )">Submit</button></p>';
于 2017-02-22T19:26:49.270 回答