简体   繁体   English

单选按钮在组中仅选择一个“是”和一个“否”选项

[英]Radio button selecting only one “YES” and one “No” option in a group

I was trying to create something similar to this: https://www.123test.com/disc-personality-test/ 我正在尝试创建与此类似的内容: https : //www.123test.com/disc-personality-test/

Only one "Yes" and one "No" options must be selected in a group, so there wont be two selections in a row or column 一组中只能选择一个“是”和一个“否”选项,因此行或列中不会有两个选择

在此处输入图片说明

I tried to create a basic html grouping: 我试图创建一个基本的html分组:

<div id="main1">
            <div id="group1">
                <input type="radio" name="group1" class="group1" value="test1"> 
                <input type="radio" name="group1" class="group1" value="test2"> Group 1
            </div>
            <div id="group2">
                <input type="radio" name="group2" class="group2" value="test1"> 
                <input type="radio" name="group2" class="group2" value="test2"> Group 2
            </div>
            <div id="group3">
                <input type="radio" name="group3" class="group3" value="test1"> 
                <input type="radio" name="group3" class="group3" value="test2"> Group 3
            </div>
            <div id="group4">
                <input type="radio" name="group4" class="group4" value="test1"> 
                <input type="radio" name="group4" class="group4" value="test2"> Group 4
            </div>
        </div>

But i still have no idea about how the Javascript / jQuery code is supposed to be like. 但是我仍然不知道Javascript / jQuery代码应该是什么样子。 If you have better suggestions for the HTML structure, please do suggest that too. 如果您对HTML结构有更好的建议,也请提出建议。 The information from this survey is supposed to be uploaded into a database. 该调查的信息应该上载到数据库中。

Try this one 试试这个

html--> HTML - >

<div id="main1">
  <div id="group1">
    <input type="radio" name="group1" class="team1" value="test1"> 
    <input type="radio" name="group1" class="team2" value="test2"> Group 1
  </div>
  <div id="group2">
    <input type="radio" name="group2" class="team1" value="test1"> 
    <input type="radio" name="group2" class="team2" value="test2"> Group 2
  </div>
  <div id="group3">
    <input type="radio" name="group3" class="team1" value="test1"> 
    <input type="radio" name="group3" class="team2" value="test2"> Group 3
  </div>
  <div id="group4">
    <input type="radio" name="group4" class="team1" value="test1"> 
    <input type="radio" name="group4" class="team2" value="test2"> Group 4
  </div>
</div>

javascript--> JavaScript的 - >

 $("input:radio").change(function(){
 var group = ":radio[name='"+ $(this).attr("name") + "']";
   if ($(this).is(':checked')) {
     $("input:radio[class^='"+ $(this).attr('class')+"']").each(function(i) {
       this.checked = false;
     });
     $(this).prop('checked', 'checked');
   }
 });

this working fine for me... 这对我来说很好

This will do the trick but you must use event binding on radio buttons to check for the condition where two radio buttons are clicked on the same row. 这可以解决问题,但是您必须在单选按钮上使用事件绑定来检查在同一行上单击两个单选按钮的情况。 jsfiddle.net/g8gvof06 jsfiddle.net/g8gvof06

Try this, 尝试这个,

<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<div id="main1">
    <div id="group1">
        <input type="radio" name="say_yes" class="group1" value="test1"> 
        <input type="radio" name="say_no" class="group1" value="test2"> Group 1
    </div>
    <div id="group2">
        <input type="radio" name="say_yes" class="group2" value="test1"> 
        <input type="radio" name="say_no" class="group2" value="test2"> Group 2
    </div>
    <div id="group3">
        <input type="radio" name="say_yes" class="group3" value="test1"> 
        <input type="radio" name="say_no" class="group3" value="test2"> Group 3
    </div>
    <div id="group4">
        <input type="radio" name="say_yes" class="group4" value="test1"> 
        <input type="radio" name="say_no" class="group4" value="test2"> Group 4
    </div>
</div>

And some Jquery, 还有一些Jquery

$('input').click(function(){
  $(this).parent().find('input').not(this).removeAttr('checked');
});

Demo http://jsbin.com/runocizugo/edit?html,js,output 演示http://jsbin.com/runocizugo/edit?html,js,输出

Thanks! 谢谢!

You have to define the YES buttons with the same name and the NO buttons with the same name to achive the vertical grouping. 您必须定义具有相同名称的YES按钮和具有相同名称的NO按钮才能实现垂直分组。 For each line I would integrate some Javascript which deselects the counterpart of the selected option. 对于每一行,我将集成一些Javascript,该Javascript将取消选择所选选项的对应内容。

 $("input:radio").change(function(){
   if ($(this).is(':checked')) {
     var name = $(this).attr("name");
     $(this).parent().children().each(function(i) { 
       if(this.getAttribute("name") != name){
         this.checked = false;
       }
     });
   }
});

https://jsfiddle.net/yeqsqxcq/ https://jsfiddle.net/yeqsqxcq/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM