2013-02-16 19 views
1

嗨我很難找出正確的方法來做到這一點,但這裏。我有3個問題,每個問題都有正確的答案。將jquery數組映射到單選按鈕

<div class="question"> 
    <h2>Question 1</h2> 
    <p>Which picture has Anna?</p> 
    <ul> 
     <li><input type="radio" id="q1_a" name="q1"><label for="q1_a">A</label></li> 
     <li><input type="radio" id="q1_b" name="q1"><label for="q1_b">B</label></li> 
     <li><input type="radio" id="q1_c" name="q1"><label for="q1_c">C</label></li> 
     <li><input type="radio" id="q1_d" name="q1"><label for="q1_d">D</label></li> 
    </ul> 
    <p class="answer"></p> 
</div> 

<div class="question"> 
    <h2>Question 2</h2> 
    <p>Person?</p> 
    <ul> 
     <li><input type="radio" id="q2_a" name="q2"><label for="q2_a">A</label></li> 
     <li><input type="radio" id="q2_b" name="q2"><label for="q2_b">B</label></li> 
     <li><input type="radio" id="q2_c" name="q2"><label for="q2_c">C</label></li> 
     <li><input type="radio" id="q2_d" name="q2"><label for="q2_d">D</label></li> 
    </ul> 
    <p class="answer"></p> 
</div> 

<div class="question"> 
    <h2>Question 3</h2> 
    <p>Alligator?</p> 
    <ul> 
     <li><input type="radio" id="q3_a" name="q3"><label for="q3_a">A</label></li> 
     <li><input type="radio" id="q3_b" name="q3"><label for="q3_b">B</label></li> 
     <li><input type="radio" id="q3_c" name="q3"><label for="q3_c">C</label></li> 
     <li><input type="radio" id="q3_d" name="q3"><label for="q3_d">D</label></li> 
    </ul> 
    <p class="answer"></p> 
</div> 

我有兩個數組組合成一個大數組的每個答案。

var incorrect = [ 
    "Hmmm, are you sure about that?", 
    "Almost! Try again.", 
    "Give it another shot.", 
    "Nice try. Just not quite nice enough.", 
    "Not this time.", 
    "You might want to check your answer.", 
    "Try again!", 
    "Oops! Pick again.", 
    "So close! Choose again.", 
    "D'oh! Choose again.", 
    "Keep trying!" 
]; 

var correct =[ 
    {id:"q1_b",text:"Hi answer",link:"www.yahoo.com"}, 
    {id:"q2_b",text:"Another good answer!",link:"www.google.com"}, 
    {id:"q3_b",text:"This is the best answer. ",link:"www.msn.com"} 
]; 

var answers = []; 
i = 0; 

answers[i] = [incorrect[10],correct[i++],incorrect[0],incorrect[5]]; 
answers[i] = [incorrect[9],correct[i++],incorrect[0]]; 
answers[i] = [incorrect[0],correct[i++],incorrect[2]]; 

的答案陣列結合了兩個數組,給予正確答案的列表,以及對這個問題的其餘部分正確的錯誤的答案。我想要做的是,映射答案數組到每個問題集,所以我不必使用每個數組,我不太確定我將如何做到這一點,鑑於它是如何兩個不同的陣列在一個。現在我沿着這些線爲每個單選按鈕:

$('input[type=radio]').click(function(){ 
    if($(this).attr("checked")){ 
     var answertext = $(this).closest('div').find('.answer'), 
      id = $(this).attr('id'), 

     if (id == answers[correct[0]['id']]){ 
      answertext.html('<span class="green">'+answers[correct[0]['text']]+'</span>'); 
     } else { 
      answertext.html('<span class="red">'+answers[incorrect[0]]+'</span>'); 
     } 

    } 
}); 

我應該使用if then語句之外的東西嗎?任何推動正確的方向將不勝感激。

編輯:編輯不正確的數組,它有文本,這不,只有正確的答案獲取文本。 編輯:重新措辭的問題。

回答

1

我能夠通過一位同事的幫助解決我的問題。我開始在每個div中添加一個數字attr:D

<div class="question" num="1"> 
<h2>Question 1</h2> 
<p>Which picture has Anna?</p> 
<ul> 
    <li><input type="radio" id="q1_a" name="q1"><label for="q1_a">A</label></li> 
    <li><input type="radio" id="q1_b" name="q1"><label for="q1_b">B</label></li> 
    <li><input type="radio" id="q1_c" name="q1"><label for="q1_c">C</label></li> 
    <li><input type="radio" id="q1_d" name="q1"><label for="q1_d">D</label></li> 
</ul> 
<p class="answer"></p> 
</div> 

<div class="question" num="2"> 
<h2>Question 2</h2> 
<p>Person?</p> 
<ul> 
    <li><input type="radio" id="q2_a" name="q2"><label for="q2_a">A</label></li> 
    <li><input type="radio" id="q2_b" name="q2"><label for="q2_b">B</label></li> 
    <li><input type="radio" id="q2_c" name="q2"><label for="q2_c">C</label></li> 
    <li><input type="radio" id="q2_d" name="q2"><label for="q2_d">D</label></li> 
</ul> 
<p class="answer"></p> 
</div> 

<div class="question" num="3"> 
<h2>Question 3</h2> 
<p>Alligator?</p> 
<ul> 
    <li><input type="radio" id="q3_a" name="q3"><label for="q3_a">A</label></li> 
    <li><input type="radio" id="q3_b" name="q3"><label for="q3_b">B</label></li> 
    <li><input type="radio" id="q3_c" name="q3"><label for="q3_c">C</label></li> 
    <li><input type="radio" id="q3_d" name="q3"><label for="q3_d">D</label></li> 
</ul> 
<p class="answer"></p> 
</div> 

然後我創建了這個來獲取索引和數字。

el = $(this).parent().index(); 
q = $(this).parent().parent().parent().attr("num"); 
resp = answers[q][el]; 

我也更新了正確的陣列,使其更簡單:

var correct =[ 
{text:"Hi answer"}, 
{text:"Another good answer!"}, 
{text:"This is the best answer."} 
]; 

的答案陣列和不正確的保持不變。

但是一個註釋:使用迭代,如它是我的++更改爲每行不起作用,如果一個問題有多個答案。如果是這樣的答案需要數組這樣寫:

answers[i++] = [incorrect[10],correct[0],incorrect[0],incorrect[5]]; 
answers[i++] = [incorrect[9],correct[1],incorrect[0]]; 
answers[i++] = [incorrect[0],correct[2],incorrect[2]]; 

最後,我更新了單選按鈕的單擊事件,像這樣:

$('input[type=radio]').click(function(){ 
if($(this).attr("checked")){ 
    var answer = $(this).closest('div').find('.answer'), 
     id = $(this).attr('id'), 

     el_number = $(this).parent().index(); 
    q_number = $(this).parent().parent().parent().attr("num"); 
    resp = answers[q_number][el_number]; 



    answer.html('<span>' + (resp["text"] != null) ? resp["text"] : resp + '</span>'); 

} 
}); 

這似乎工作。 :D