javascript checkbox enable/disable

2019-03-30 18:16发布

问题:

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 = 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.

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

<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>

回答1:

Try instead:

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


回答2:

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

$(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:

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