2017-02-22 57 views
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; 
} 
+0

請粘貼代碼和標記導致該問題的線路。 –

+0

這很可能不是您在此粘貼的代碼,因爲我可以複製並粘貼該代碼而不會收到相同的錯誤。顯示更多周圍的代碼 - 這可能是導致這一問題的一兩個問題。 – Jason

+0

你有不同的代碼行嗎?由於您正在構建一個字符串,因此可能會導致該問題。因爲js行工作正常https://jsfiddle.net/cmhb465u/15/ – Imprfectluck

回答

1

你不能有一個函數調用是這樣的:

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

您需要評估在一個單獨的可變參數,然後通過它的功能。

0

因此,在您的onAction multiAnswer調用中,您將3個輸入用引號括起來。引用您的multiAnswer函數後,您確實有3個您正在尋找的輸入。這些輸入的末尾還有+號。你不需要連接函數調用中的parens。

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

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