2014-11-07 124 views
0

選擇是在json對象中的嵌套數組我選擇這個數據爲多選測驗序列。 當我按功能觸發器控制檯說'未捕獲TypeError:無法讀取屬性的'選擇'的未定義'我也有一個變量'c',當用戶選擇另一個考試,所以語句考試=考試+ c將無法安置功能的工作。錯誤與JSON未捕獲TypeError:無法讀取未定義的屬性'選擇'

var exam0 = [ 
    { 
     "question": "which is a negative number?", 
     "choices": [ 
         "2", 
         "-2",`` 
         "6", 
         "8", 
     ], 
     "correctAnswer": "B", 
     "hint": "The one with the ' - ' negative sign" 
    }, 
    ......other questions**** 
  
]; 

功能是

function placement(x) /*x is variable used to change the question from the json object*/ 
{ 
    choiceOne=$('<p>').text('A.'+exam[x].choices[0]); /*where the console points to problem*/ 
    choiceTwo=$('<p>').text('B.'+exam[x].choices[1]); 
    choiceThree=$('<p>').text('C.'+exam[x].choices[2]); 
    choiceFour=$('<p>').text('D.'+exam[x].choices[3]); 
    currentQuestion=$('<p>').text(exam[x].question); 
    $("#honeyPot").empty().append(currentQuestion); 
    $("#honeyPot p").prepend(count+"."); 
    $('#options').find('p').remove().hide(); 
    $('#optionOne').fadeIn(250).append(choiceOne); 
    $('#optionTwo').fadeIn(250).append(choiceTwo);  
    $('#optionThree').fadeIn(250).append(choiceThree); 
    $('#optionFour').fadeIn(250).append(choiceFour);  
} 
+0

您正在定義'exam0',但'placement'函數引用'exam'。它看起來並不像任何地方定義「考試」。 – sherb 2014-11-07 03:10:34

回答

0

我想既然exam0是JSON對象,你應該在你的函數,而不是過多的考試使用exam0。

function placement(x) /*x is variable used to change the question from the json object*/ 
{ 
    choiceOne=$('<p>').text('A.'+exam0[x].choices[0]); /*where the console points to problem*/ 
    choiceTwo=$('<p>').text('B.'+exam0[x].choices[1]); 
    choiceThree=$('<p>').text('C.'+exam0[x].choices[2]); 
    choiceFour=$('<p>').text('D.'+exam0[x].choices[3]); 
    currentQuestion=$('<p>').text(exam0[x].question); 
    $("#honeyPot").empty().append(currentQuestion); 
    $("#honeyPot p").prepend(count+"."); 
    $('#options').find('p').remove().hide(); 
    $('#optionOne').fadeIn(250).append(choiceOne); 
    $('#optionTwo').fadeIn(250).append(choiceTwo);  
    $('#optionThree').fadeIn(250).append(choiceThree); 
    $('#optionFour').fadeIn(250).append(choiceFour);  
} 
+0

我還有一個變量'c',當用戶選擇另一個考試時會發生變化,所以考試考試=考試+ c將無法實現安置職能。 – user3697958 2014-11-07 14:54:32

相關問題