Javascript to check whether a checkbox is being checked or unchecked

前端 未结 7 1174
轮回少年
轮回少年 2020-11-29 06:37

I have a javascript routine that is performing actions on a group of checkboxes, but the final action I want to set the clicked checkbox to checked or unchecked based on if

7条回答
  •  一整个雨季
    2020-11-29 07:03

    The value attribute of a checkbox is what you set by:

    
    

    So when someone checks that box, the server receives a variable named test with a value of 1 - what you want to check for is not the value of it (which will never change, whether it is checked or not) but the checked status of the checkbox.

    So, if you replace this code:

    if (arrChecks[i].value == "on") 
    {
        arrChecks[i].checked = 1;
    } else {
        arrChecks[i].checked = 0;
    }
    

    With this:

    arrChecks[i].checked = !arrChecks[i].checked;
    

    It should work. You should use true and false instead of 0 and 1 for this.

提交回复
热议问题