【问题标题】:enable and disable of check box is not disabling the button启用和禁用复选框不会禁用按钮
【发布时间】:2014-10-07 09:44:51
【问题描述】:

下面是我的代码:

根据我的功能,当我选中我的复选框时,必须禁用按钮,当我取消选中复选框时,必须启用复选框。

function assignDefect() {          
    var new_tc_arry = new Array();     
    var checkbox = document.getElementsByName('radSize1');    
    for(var i=0;i<checkbox.length;i++){   
        if(!checkbox[i].disabled && checkbox[i].checked){  
            var checkedbox = checkbox[i].value;            
            new_tc_arry.push(checkbox[i].value);    
            for(var i=0;i<new_tc_arry.length;i++){    
                console.log("new_tc_arry["+i+"] -->"+new_tc_arry[i]);    
                document.getElementById("OrphanbuttonId").disabled =true;   
            }    
        }    
        else {    
        document.getElementById("OrphanbuttonId").disabled=false;                                                                                      
        }   
}      

The above code is disabling my button when i check my last checkbox.
Its not working for 1st ,2nd..etc checkboxes.**

【问题讨论】:

  • 首先设置一个警报,看看是否在选中前两个复选框时触发了该功能。否则检查迭代器
  • console.log("new_tc_arry["+i+"] -->"+new_tc_arry[i]); ---> 正在控制台上打印检查的值...

标签: javascript


【解决方案1】:

问题在于,即使您禁用了第一个、第二个等按钮(不包括最后一个复选框),您仍会继续执行 for 循环。基本上,如果您的最后一个复选框未选中,则该按钮将始终重新启用。尝试在你的 for 循环中添加一个 break 语句:

function assignDefect() {          
    var new_tc_arry = new Array();     
    var checkbox = document.getElementsByName('radSize1');    
    for(var i=0;i<checkbox.length;i++){   
        if(!checkbox[i].disabled && checkbox[i].checked){  
            var checkedbox = checkbox[i].value;            
            new_tc_arry.push(checkbox[i].value);    
            for(var i=0;i<new_tc_arry.length;i++){    
                console.log("new_tc_arry["+i+"] -->"+new_tc_arry[i]);    
                document.getElementById("OrphanbuttonId").disabled =true;   
            }
            break; //<---------------------------------------CHANGE HERE    
        }    
        else {    
        document.getElementById("OrphanbuttonId").disabled=false;                                                                                      
        }   
    }
}

【讨论】:

  • 很高兴 :) 如果您满意,如果您将其标记为答案,将不胜感激!干杯!
猜你喜欢
  • 2020-12-28
  • 1970-01-01
  • 2018-11-07
  • 2015-08-02
  • 2021-08-21
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
相关资源
最近更新 更多