[英]Making a simple multiple choice quiz HTML/JQUERY
I'm currently making a basic review test for myself and it looks something like this: 我目前正在为自己做一次基本的检查测试,它看起来像这样:
HTML - HTML-
<form class="q">
<h4>1. question 1 </h4>
<input name="test" type="radio" value="inc" /> A) 1
<input name="test" type="radio" value="ans"/> B) 2
<input name="test" type="radio" value="inc" /> C) 3
<input name="test" type="radio" value="inc" /> D) 4
<input name="test" type="radio" value="inc" /> E) 5
</form>
<br />
<div class="exp"> a is right </div>
<div class="red"> a is the correct answer</div>
<form class="q">
<h4>1. question 2 </h4>
<input name="test" type="radio" value="inc" /> A) 1
<input name="test" type="radio" value="ans"/> B) 2
<input name="test" type="radio" value="inc" /> C) 3
<input name="test" type="radio" value="inc" /> D) 4
<input name="test" type="radio" value="inc" /> E) 5
</form>
<br />
<div class="exp"> d is right </div>
<div class="red"> d is the correct answer</div>
jQuery - jQuery-
$(function () {
$('input[name="test"]').on('click', function() {
if ($(this).val() == 'ans') {
$('.exp').show();
$('.red').hide();
} else {
$('.exp').hide();
$('.red').show();
}
})
});
I have it setup so that when the user selects the correct or incorrect answer, a message will popup. 我进行了设置,以便当用户选择正确或不正确的答案时,将弹出一条消息。
My problem is that when the user clicks an answer in the first question whether it be correct or incorrect, it displays a message for both questions instead of just the single question. 我的问题是,当用户单击第一个问题的答案是正确还是错误时,它会显示两个问题的消息,而不仅仅是单个问题。 I was wonder how I could make it work for each question instead of all of them at once. 我想知道如何才能使它适用于每个问题,而不是一次解决所有问题。
Try this: 尝试这个:
<div class="exp answer-text" style="display:none"> d is right </div>
<div class="red answer-text" style="display:none"> d is the correct answer</div>
$(function () {
$('input[name="test"]').on('click', function() {
$(".answer-text").hide();
if ($(this).val() == 'ans') {
$('.exp').show();
} else {
$('.red').show();
}
})
});
You can use jQuery method chaining to select the first next div to the parent. 您可以使用jQuery方法链来选择父级的下一个div。 : :
el.parents('.q').nextAll('.exp').first();
Example Snippet: 示例片段:
$(function() { $('.exp').hide(); $('.red').hide(); $('input[name="test"]').on('click', function() { var el = $(this); if (el.val() == 'ans') { el.parents('.q').nextAll('.exp').first().show(); el.parents('.q').nextAll('.red').first().hide(); $('.red').hide(); } else { el.parents('.q').nextAll('.red').first().show(); el.parents('.q').nextAll('.exp').first().hide(); } }) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="q"> <h4>1. question 1 </h4> <input name="test" type="radio" value="inc" />A) 1 <input name="test" type="radio" value="ans" />B) 2 <input name="test" type="radio" value="inc" />C) 3 <input name="test" type="radio" value="inc" />D) 4 <input name="test" type="radio" value="inc" />E) 5 </form> <br /> <div class="exp">a is right</div> <div class="red">a is the correct answer</div> <form class="q"> <h4>1. question 2 </h4> <input name="test" type="radio" value="inc" />A) 1 <input name="test" type="radio" value="ans" />B) 2 <input name="test" type="radio" value="inc" />C) 3 <input name="test" type="radio" value="inc" />D) 4 <input name="test" type="radio" value="inc" />E) 5 </form> <br /> <div class="exp">d is right</div> <div class="red">d is the correct answer</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.