简体   繁体   English

参数列表后不断出现SyntaxError:missing)

[英]Keep getting SyntaxError: missing ) after argument list

I keep getting this error due to these two lines: 由于这两行,我不断收到此错误:

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

And I can't figure out what I am missing . 而且我无法弄清我缺少什么。

Edit: Here is the surrounding code (Excuse the mess) Contains methods that uses a switch statement to determine the input for the arrays required, from there puts it into the parameters for DisplayQuestion which then passes it to the functions below from the behaviour wanted: 编辑:这是周围的代码(不好意思),包含使用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;
 }

You cannot have a function call like this: 您不能有这样的函数调用:

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

You will need to evaluate the parameter in a seperate variable and then pass it in the function. 您将需要在单独的变量中评估参数,然后将其传递给函数。

So in your onClick call of multiAnswer you have wrapped the 3 inputs in quotes. 因此,在multiAnswer的onClick调用中,您已将3个输入括在引号中。 After referencing your multiAnswer function you do have the 3 inputs that you are looking for. 引用multiAnswer函数后,您确实具有要查找的3个输入。 You also have + signs on the ends of those inputs. 在这些输入的末尾也有+号。 You do not need to concatenate the parens inside of the function call. 您无需在函数调用内连接括号。

I hope this helps! 我希望这有帮助! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM