【问题标题】:disabling and enabling the radio buttons禁用和启用单选按钮
【发布时间】:2016-09-12 10:57:56
【问题描述】:

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

 <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

【问题讨论】:

  • 更好的方式使用#jquery #Toggle

标签: javascript


【解决方案1】:

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

见下面的例子

 <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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2011-09-15
    • 1970-01-01
    • 2020-10-18
    • 2012-10-17
    • 2012-01-05
    • 2010-09-05
    相关资源
    最近更新 更多