开发者

Javascript function to enable submit button when checkboxes are checked

开发者 https://www.devze.com 2023-03-26 10:58 出处:网络
I am trying to write a Javascript function that will be called whenever a checkbox is clicked, and will check how many checkboxes are checked in total, and will then either enable or disable the form

I am trying to write a Javascript function that will be called whenever a checkbox is clicked, and will check how many checkboxes are checked in total, and will then either enable or disable the form submit button, depending on how many checkboxes are checked.

What I have is of this form:

<html>
<head>

<script type="text/javascript" language="JavaScript">

function enabledisablenext()
{
    var boxeschecked = 0;
    for (var i = 0; i< 99; i++) {
        if(document.getElementById("transcheck" + i).checked == true){ 
        boxeschecked ++;
        }
    }
    if (boxeschecked == 0) {document.getElementById("next").disabled= true;}
    if (boxeschecked != 0) {document.getElementById("next").disabled= false;}
}
</script>

</head>

<body>

<form action='something.php' method='POST'>

<input type='checkbox' id='transcheck1' onclick="enabledisablenext()"/>
<input type='checkbox' id='transcheck2' onclick="enabledisablenext()"/>
<input type='checkbox' id='transcheck3' onclick="enabledisablenext()"/>
<input type='checkbox' id='transcheck4' onclick="enabledisablenext()"/>
<input type='checkbox' id='transcheck5' onclick="enabledisablenext()"/>

<input type='submit' disabled='disabled' id='next'/><p></p>

</form>                          

</body>
</html>
开发者_如何学C

However, this isn't working. The submit button is not enabled when 1 or more checkboxes are checked. Can someone see where I went wrong?

Thanks!


Check this

http://jsfiddle.net/MYwJD/1/

In your example the i variable needs to be of range 1 to number of inputs, otherwise it throws an error.

0

精彩评论

暂无评论...
验证码 换一张
取 消