在参数列表后继续获取SyntaxError:missing)

时间:2017-02-22 18:52:20

标签: javascript debugging web-developer-toolbar

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

    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;
 }

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

因此,在您对onAnswer的onClick调用中,您已将3个输入包装在引号中。在参考multiAnswer功能后,您可以获得所需的3个输入。您还可以在这些输入的末尾添加+符号。您不需要连接函数调用中的parens。

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

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