首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查(动态)选中了哪个单选按钮?

如何检查(动态)选中了哪个单选按钮?
EN

Stack Overflow用户
提问于 2016-10-10 19:00:52
回答 3查看 88关注 0票数 0

我正在创建一个在线考试门户,下面是向考生显示问题的代码。我不知道如何处理考生点击的答案

代码语言:javascript
复制
$("input[name='hello']").click(function(){
    alert("you clicked on");
});

单选按钮是为100个问题动态创建的,每个问题4个

EN

回答 3

Stack Overflow用户

发布于 2016-10-10 19:44:37

试试这个-

代码语言:javascript
复制
$('input[data-type=choice]').change(function() {
  var Question = $(this).attr('name');
  var Checked = $(this).attr('value');
  console.log('Selected Choice for ' + Question + ' is ' + Checked);
});
代码语言:javascript
复制
<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>

票数 2
EN

Stack Overflow用户

发布于 2016-10-10 19:32:37

考虑每个选项的值

然后使用下面的代码来了解单击的是哪一个:

代码语言:javascript
复制
$("input[name='hello']:checked").val();

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

代码语言:javascript
复制
$("input[name='hello']").click(function(){
   var v=$("input[name='hello']:checked").val();
   alert("you clicked on"+v);
});
票数 0
EN

Stack Overflow用户

发布于 2016-10-10 19:36:36

假设你有一个包含每4个框的questionWrapper类的div,你可以这样做:

代码语言:javascript
复制
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
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39956766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档