2016-10-10 132 views
0

我创建一个在线考试门户以下是显示问题,考生的代码。我不知道如何处理由考生如何检查哪个单选按钮(动态)被检查?

$("input[name='hello']").click(function(){ 
    alert("you clicked on"); 
}); 

单选按钮是动态的100个问题4各

+2

你这是什么意思是__handle__? – Satpal

+0

只是我每个显示100个问题4个选项。并让考生回答这些问题。在这里,我试图评估候选人天气的右边错误所检查的问题,并最终显示得分。 –

+1

SO – Satpal

回答

0

创建点击答案考虑一个值为每个选项

然后用它来了解其中一个被点击:

$("input[name='hello']:checked").val(); 

你可以重写你这样的代码:

$("input[name='hello']").click(function(){ 
    var v=$("input[name='hello']:checked").val(); 
    alert("you clicked on"+v); 
}); 
0

假设你有没有和身边每4个箱questionWrapper类股利你可以做这样的事情:

var collection = $(".questionWrapper"); 
$.each(collection, function(i, $questionWrapper, function(){ 
    //Do this for all the questions 
    //Find the checked input (maybe use classes here...) and get its value 
    var clickedAnswer = $($questionWrapper).find("input:checked").val(); 
    //Do something with the clicked var, maybe push it to an array and then compare the array to the solution 
}); 
2

试一试 -

$('input[data-type=choice]').change(function() { 
 
    var Question = $(this).attr('name'); 
 
    var Checked = $(this).attr('value'); 
 
    console.log('Selected Choice for ' + Question + ' is ' + Checked); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="button" value="Log all Answers" onclick="logAllAnswers()"> 
 
<input type="button" value="Clear Log" onclick="console.clear();"> 
 
<hr> 
 
<form> 
 
    <fieldset> 
 
    <legend>1. Select the answer for the first question.</legend> 
 
    <input type="radio" data-type="choice" name="Q1" value="1">Option 1 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q1" value="2">Option 2 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q1" value="3">Option 3 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q1" value="4">Option 4 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>2. Select the answer for the second question.</legend> 
 
    <input type="radio" data-type="choice" name="Q2" value="1">Option 1 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q2" value="2">Option 2 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q2" value="3">Option 3 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q2" value="4">Option 4 
 
    </fieldset> 
 
    <fieldset> 
 
    <legend>3. Select the answer for the third question.</legend> 
 
    <input type="radio" data-type="choice" name="Q3" value="1">Option 1 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q3" value="2">Option 2 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q3" value="3">Option 3 
 
    <br> 
 
    <input type="radio" data-type="choice" name="Q3" value="4">Option 4 
 
    </fieldset> 
 
</form> 
 

 

 

 
<script> 
 
    function logAllAnswers() { 
 
    $('input[data-type=choice]:checked').each(function() { 
 
     var Question = $(this).attr('name'); 
 
     var Checked = $(this).attr('value'); 
 
     console.log('Selected Choice for ' + Question + ' is ' + Checked); 
 
    }); 
 
    } 
 
</script>

+0

data-type =“choise”不支持 –

+0

获得选定的单选按钮值你的意思是不支持。你使用jQuery吗? - 检查结果中的代码段的作品。 –

+0

在我的编辑记事本+ +的数据类型=“的choise”在黑色显示属性,我认为它不支持。 雅其片段,但本地主机它不是回环 –