2011-11-28 6 views
1

안녕하세요이 다차원 배열에 대한 나의 목표는 사용자가 복용하고있는 테스트에 대한 답변 (옳고 그른지)을 문서화하는 것입니다. 처음에는 테스트 배열이 설정되었습니다. 사용자가 앱이 배열에 추가해야하는 질문을 통과하면됩니다. 참고 : 페이지가 다시로드되지 않습니다. 질문은 아약스를 통해 이루어집니다. 여기 배열 내의 자바 스크립트 배열 오류

내가 무엇을 가지고 지금까지 내가 시험 배열이 난 다음 함수를 추가해야하는 값을 얻기 위해 무엇도 너무

test[0] 
    test[1][0] = question 1 
    test[1][1] = correct 
    test[2][0] = question 4 
    test[2][1] = incorrect 
    test[3][0] = question 17 
    test[3][1] = correct 
    test[4][0] = question 12 
    test[4][1] = incorrect 



    test[0] 
    test[1][0] = question 1 
    test[1][1] = correct 
    test[2][0] = question 2 
    test[2][1] = incorrect 
    test[3][0] = question 3 
    test[3][1] = correct 
    test[4][0] = question 4 
    test[4][1] = incorrect 

같은 데이터를 보유 할

var test = new array(); 

    function nextquestion(){ 
     var result; 

    if (document.getElementById('rb2').checked = true){ 
    result = 'correct' ; 
    } 
else{ 

result = 'incorrect'; 
} 
var num = document.getElementByName('rb').value; 

var question =new Array(); 
question1[0]= document.getElementByName('rb').value; //question id  
question1[1]= result; 

질문 ID 그래서 내가 데이터베이스에서 다음 질문을 얻을 수 +1

미리 감사! 다른 배열을 유지하기 위해 test 배열의 요소를 원하는 경우

+1

'경우 (document.getElementById를 ('RB2'에서). checked = true)'문제가된다.'.checked = true' (할당)를 말하지 말고'.checked === true' (비교)라고 말한다. 또는'.checked' 속성이 부울이라면 비교할 필요가 없습니다 : if (document.getElementById ('rb2'). checked) {}'. 또한 코드의 첫 번째 줄은 'A'를 대문자로 사용하여'Array()'를 참조하거나, 더 나은 방법 :'var test = []'을 참조해야합니다. – nnnnnn

답변

2

, 당신은 수동으로 설정해야합니다 :

var test = []; 
test.push([]); 
test[0].push("Question 1"); 
test[0].push(true); //or "correct" 

var q1 = test[0][0]; 
//"Question 1" 

var wasQ1Correct = test[0][1]; 
//true 

그래서

+0

고마워요 soooo 많이 –

+0

@ user1068658 - 내 기쁨 - 다행 당신을 위해 일했다 –