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語句之外的東西嗎?任何推動正確的方向將不勝感激。
編輯:編輯不正確的數組,它有文本,這不,只有正確的答案獲取文本。 編輯:重新措辭的問題。