简体   繁体   English

如何强制复选框的行为像radiobutton,双循环

[英]How to force checkbox to behave like radiobutton, double loop

I have checkboxes in 2 for loops. 我在2 for循环中有复选框。 I want to force them to behave like radiobuttons. 我想强迫他们表现得像无线电按钮。 There should be only one option chosen for each Q (first loop). 每个Q(第一个循环)应该只选择一个选项。 I have a peace of code, but it doesn't work. 我有一个代码的和平,但它不起作用。 How to change it? 怎么改呢?

script type="text/javascript">  

    var classes = $('.myDiv').map(function () {
        return $(this).attr('class');
    }).get();

    $.each(classes, function(i) {
        $(classes[i]).change(function() {
            var checked = $(this).is(':checked');
            $(classes[i]).prop('checked',false);
            if(checked[i]) {
                $(this).prop('checked',true);
            }
        });
    });  

Add a common class to the checkbox input and then use that class like below 在复选框输入中添加一个公共类,然后像下面一样使用该类

 $("input:checkbox").change(function() { $(this).siblings().prop('checked', false); $(this).prop('checked', true); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> </td> </tr> <tr> <td> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> <input type="checkbox" value="1" /> </td> </tr> </table> 

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

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