我正在创建一个在线考试门户,以下是向考生显示问题的代码。我不知道如何处理候选人点击的答案
$("input[name='hello']").click(function(){
alert("you clicked on");
});
单选按钮是针对 100 个问题动态创建的,每个问题 4 个问题
最佳答案
试试这个 -
$('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>
关于javascript - 如何检查(动态)选中哪个单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39956766/