禁用和启用单选按钮

disabling and enabling the radio buttons

本文关键字:单选按钮 启用      更新时间:2023-09-26

我有一个复选框和两个单选按钮。当我选中复选框。我希望这两个单选按钮启用,当我取消复选框,我希望这两个单选按钮得到禁用下面是我尝试过的代码

 <script>
 function myfunction()
 {
 var radio=document.getElementsByName("disableme");
 var len=radio.length;
 for(var i=0;i<len;i++)
 {
   radio[i].disabled=true;
 }
 }
 </script>
 Notification
 <input type="checkbox" onclick=myfunction() checked>
                <input type="radio" name="disableme" id="1"> open
                <input type="radio" name="disableme" id="2">close

您应该检查是否选中了checkbox -为了做到这一点,您可以在调用函数时传递元素。

参见下面的例子

 <script>
     function myfunction(el) {
         var radio=document.getElementsByName("disableme");
         var len=radio.length;
         for(var i=0;i<len;i++) {
             radio[i].disabled=!el.checked;
         }
     }
 </script>
Notification
<input type="checkbox" onclick=myfunction(this) checked>
            <input type="radio" name="disableme" id="1">open
            <input type="radio" name="disableme" id="2">close