2016-10-19 20 views
3

我设置了响应由单选按钮标记处理选择题调查问卷A,B,C和D.获取单选按钮的值设置

我想找出答案,每个通过获取表单ID和单击的按钮值来提出问题,例如,使用第一组单选按钮的响应可能是1B和第二,2D等等(表单ID脚本已经启动并运行)。

我需要这与多组单选按钮

这里的页面上工作是HTML:

<form class="toeic_pages" id="1"> 
     <label class="item1"> 
     <input type="radio" value="A" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
     <label class="item2"> 
     <input type="radio" value="B" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
     <label class="item3"> 
     <input type="radio" value="C" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
     <label class="item4"> 
     <input type="radio" value="D" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
    </form> 
    ...                 
    <form class="toeic_pages" id="2"> 
     <label class="item1"> 
     <input type="radio" value="A" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
     <label class="item2"> 
     <input type="radio" value="B" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
     <label class="item3"> 
     <input type="radio" value="C" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
     <label class="item4"> 
     <input type="radio" value="D" name="toeic"/> 
     <div class="radio-image"></div> 
     </label> 
    </form> 

不过,虽然我已经能够用得到检查单选按钮值这个:

jQuery('input[name=toeic]').change(function(){ 
     var invar = jQuery('input[name=toeic]:checked').val(); 
     alert(invar); 
    }); 

该脚本只在点击的第一行按钮内有效。

为了说明,如果在要访问的第一行按钮中点击按钮B,则显示B(正确)。

但是,如果在另一行中,点击了按钮C,则B显示(不正确)..并且B继续显示以用于所有后续的按钮点击。我已经检查了这个SO网页,但我一直没有找到解决方案 - 这个问题似乎是多套单选按钮。

任何帮助将非常赞赏

+1

'$(this).closest('form')。find('input [name = toeic]:checked')。val()' – Tushar

+0

我把这个放在var invar赋值的RHS中,但它不起作用。 – RoyS

回答

0

你可以做这样的事情:

$(document).ready(function() { 
 
    $("#finish").click(function() { 
 
    var answersList = []; 
 
    //Loop over all questoins 
 
    $(".toeic_pages").each(function() { 
 

 
     var questionId = $(this).attr("id"); 
 
     var answer = $("input[name='toeic']:checked", $(this)).val(); 
 

 
     //if Answer isnt provided do not update the answersList 
 
     if (answer !== undefined) { 
 
     answersList.push({ 
 
      question: questionId, 
 
      answer: answer 
 
     }); 
 
     } 
 
    }); 
 
    console.log(answersList); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form class="toeic_pages" id="1"> 
 
    <label class="item1"> 
 
    <input type="radio" value="A" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
    <label class="item2"> 
 
    <input type="radio" value="B" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
    <label class="item3"> 
 
    <input type="radio" value="C" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
    <label class="item4"> 
 
    <input type="radio" value="D" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
</form> 
 
... 
 
<form class="toeic_pages" id="2"> 
 
    <label class="item1"> 
 
    <input type="radio" value="A" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
    <label class="item2"> 
 
    <input type="radio" value="B" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
    <label class="item3"> 
 
    <input type="radio" value="C" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
    <label class="item4"> 
 
    <input type="radio" value="D" name="toeic" /> 
 
    <div class="radio-image"></div> 
 
    </label> 
 
</form> 
 

 
<button id="finish">Get Answers</button>

+0

感谢您预测问题/答案格式并将其放入代码。工作很好。我非常感谢你的帮助罗伊 – RoyS

0

我认为这正是你需要的。

$('#1 input').on('change', function() { 
 
     alert($('#1').attr('id')+$('input[name=radioName]:checked', '#1').val()); 
 
    }); 
 
    $('#2 input').on('change', function() { 
 
     alert($('#2').attr('id')+$('input[name=radioName]:checked', '#2').val()); 
 
    }); 
 
    $('#3 input').on('change', function() { 
 
     alert($('#3').attr('id')+$('input[name=radioName]:checked', '#3').val()); 
 
    }); 
 
    $('#4 input').on('change', function() { 
 
     alert($('#4').attr('id')+$('input[name=radioName]:checked', '#4').val()); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <form id="1"> 
 
    Question 1 
 
    <br> 
 
    <input type="radio" name="radioName" value="A" /> A <br /> 
 
    <input type="radio" name="radioName" value="B" /> B <br /> 
 
    <input type="radio" name="radioName" value="C" /> C <br /> 
 
    </form> 
 
    <form id="2"> 
 
     Question 2 
 
     <br> 
 
    <input type="radio" name="radioName" value="A" /> A <br /> 
 
    <input type="radio" name="radioName" value="B" /> B <br /> 
 
    <input type="radio" name="radioName" value="C" /> C <br /> 
 
    </form> 
 
    <form id="3"> 
 
     Question 3 
 
     <br> 
 
    <input type="radio" name="radioName" value="A" /> A <br /> 
 
    <input type="radio" name="radioName" value="B" /> B <br /> 
 
    <input type="radio" name="radioName" value="C" /> C <br /> 
 
    </form> 
 
    <form id="4"> 
 
     Question 4 
 
     <br> 
 
    <input type="radio" name="radioName" value="A" /> A <br /> 
 
    <input type="radio" name="radioName" value="B" /> B <br /> 
 
    <input type="radio" name="radioName" value="C" /> C <br /> 
 
    </form>

学分这个答案@Peter j的业主在这个问题上How can I know which radio button is selected via jQuery?

+0

这不适用于我描述页面上有多组单选按钮的情况,比如每组10个包含与10个问题对应的4个按钮。我的确看过@Peter J的回答。谢谢 – RoyS

+0

也许我没有清楚地表达自己。我在页面上有多个单选按钮,上面有几个表单。我形成了> 1组单选按钮。每个表格都与一个问题相关联,单选按钮是另一种答案 - 一个经典的多项选择问卷。问题是,如上所述,单击页面上任意位置的初始单选按钮可防止页面上其他表单中的点击访问值,任何帮助都将非常有帮助。我真的被这个问题困扰了。 – RoyS

+0

我编辑它,我认为通过这样做可以帮助你,但根据你所说的,你正在寻找差异。回答。那么,goodluck,并希望你找到最好的答案:) – Sytor