Javascript javascript复选框启用/禁用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5215048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:12:16  来源:igfitidea点击:

javascript checkbox enable/disable

javascripthtmllistboxcheckbox

提问by Dejan Stuparic

Ok this is very anoying, and it is probably very simple. I want to start my web page with disabled checkboxes, and after particlar line in listbox is selected to enable those boxes. So I put this in onload method

好吧,这很烦人,而且可能很简单。我想用禁用的复选框启动我的网页,并在选择列表框中的特定行以启用这些框之后。所以我把它放在 onload 方法中

onload = function () {
   for (i = 0; i < document.frmMain.checkgroup.length; i++){
        document.frmMain.checkgroup[i].disabled = true ;
   }
}

it start my page with disabled boxes, now i want do enable them

它以禁用的框开始我的页面,现在我想启用它们

function enableCheckboxes(){
    if (document.frmMain.Vrste[document.frmMain.Vrste.selectedIndex].value == "Sendvici i Rostilj"){
        for(i=0;i<document.frmMain.checkgroup.length;i++){
       document.frmMain.checkgroup[i].enabled = true;

        }
    }
}

it goes in to the for loop, but it never enable those checkboxes. I cannot figure it why.

它进入 for 循环,但它从不启用这些复选框。我想不通为什么。

and this is html part, where i call enablecheckbox function:

这是 html 部分,我在其中调用 enablecheckbox 函数:

<select name="Vrste" onChange="PopulatePodvrste(); enableCheckboxes();"  size="8">
    <option value="Pica">Pica</option>
    <option value="Barbarina domaca trpeza">Barbarina domaca trpeza</option>
    <option value="Slana Palacinka">Slana Palacinka</option>
    <option value="Slatka Palacinka">Slatka Palacinka</option>
    <option value="Sendvici i Rostilj">Rostilj i sendvici</option>
    <option value="Dobro jutro sa Barbarom">Dobro jutro sa Barbarom</option>
    <option value="Chicken Meni">Chicken Meni</option>
    <option value="Posebna Ponuda">Posebna Ponuda</option>
    <option value="Salate">Salate</option>
</select>

And finally actual checkboxes:

最后是实际的复选框:

<input type="checkbox" name="checkgroup" >Susam</input><br>
<input type="checkbox" name="checkgroup" >Cili</input><br>
<input type="checkbox" name="checkgroup" >Tartar</input><br>
<input type="checkbox" name="checkgroup" >Urnebes</input><br>
<input type="checkbox" name="checkgroup" >Krastavac</input>

回答by Edward Z. Yang

Try instead:

试试吧:

    document.frmMain.checkgroup[i].disabled = false ;

回答by Avitus

if would add the jquery library to your page then I would add:

如果要将 jquery 库添加到您的页面,那么我会添加:

$(document).ready(function() {
    $("input[name='checkgroup']").attr("disabled", "disabled");
})

function enableCheckboxes() {
   $("input[name='checkgroup']").removeAttr("disabled");
}

If you don't want to use jquery then just change your enable line to be:

如果您不想使用 jquery,那么只需将启用行更改为:

document.frmMain.checkgroup[i].disabled = false ;