繁体   English   中英

选择多个单选按钮

[英]Selecting multiple radio buttons

好吧,我在 php 中有一个 for 循环,它为一个人生成一组单选按钮,组中的每个单选按钮都具有相同的名称。

for ($i = 0; $i < $count; $i++) {
            echo '<tr>';
            echo '<td>' . $something[$i] . '</td>';
            echo '<td align="center"><input class="one" type="radio" name="' . $i . '" value="1"/></td>';
            echo '<td align="center"><input class="half" type="radio" name="' . $i . '" value="0.5"/></td>';
            echo '<td align="center"><input class="zero" type="radio" name="' . $i . '" value="0" checked="checked"/></td>';
            echo '<td align="center"><input class="inactive" type="radio" name="' . $i . '" value="null"/></td>';
            echo '</tr>';
        }

我希望能够通过单击链接/按钮来选择/检查具有相同类的所有单选按钮。 变量$count每隔几天就会改变一次,所以我不知道会有多少个不同的广播组。 我正在寻找这个希望在javascript中

你不能这样做。 单选按钮组允许单选。 您将需要复选框来完成此操作。

如果类名对于单选按钮是唯一的,您可以这样做:

var radios = document.getElementsByTagName("INPUT");
for (var index = 0; index < radios.length; index++) {
   if (radios(index).type == "radio" && 
       radios(index).className.indexOf("one") > -1) {
      radios(index).checked = true;
   }
}

所有这一切可能并不比对每个都使用 getElementsById 好,但可能比 getElementsByClassName 好,我不认为这是普遍支持的。

好吧,我在这里弄清楚了,

            $(document).ready(function() {

            // select all radio buttons in the "one" column (.one class)
            $('#select_all_one').click(function(){
                $("input[class=one]").each(function(){
                    this.checked = true;
                });
            });

            // select all radio buttons in the "half" column (.half class)
            $('#select_all_half').click(function(){
                $("input[class=half]").each(function(){
                    this.checked = true;
                });
            });

            // select all radio buttons in the "zero" column (.zero class)
            $('#select_all_zero').click(function(){
                $("input[class=zero]").each(function(){
                    this.checked = true;
                });
            });

            // select all radio buttons in the "inactive" column (.inactive class)
            $('#select_all_inactive').click(function(){
                $("input[class=inactive]").each(function(){
                    this.checked = true;
                });
            });


          });

然后选择所有收音机,我使用此链接<a id="select_all_one" name="select_all" value="all_one">并将 id 的名称和值更改为一半、零和非活动链接,一切都很完美

暂无
暂无

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

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