简体   繁体   中英

How to enable the submit button when the checkbox is checked with javascript?

The form's submit button should be disabled until the user has checked the checkbox to say that they have read and agree to the terms and conditions Here is code html code for checkbox and submit button.I've tried this but failed

 var submit = document.getElementsByName('termsChkbx')[0]; input.onchange=function() { if(input.checked) { document.getElementsByName('submit').disabled = false; } else { document.getElementsByName('submit').disabled = true; } }
 <p style="color: #FF0000; font-weight: bold;" id='termsText'>I have read and agree to the terms and conditions <input type="checkbox" name="termsChkbx"></p> <input type="submit" name="submit" value="Book now!" disabled>

Here where you got it wrong :

You put your checkbox reference to variable "submit". Then you used another variable named "input" where there is no value in it.

Updated Answer :

 <p style="color: #FF0000; font-weight: bold;" id='termsText'>I have read and agree to the terms and conditions <input type="checkbox" name="termsChkbx"></p> <input type="submit" name="submit" value="Book now!" disabled> <script> var input = document.getElementsByName('termsChkbx')[0]; var submit = document.getElementsByName('submit')[0]; input.onchange=function() { if(input.checked) { submit.disabled = false; } else { submit.disabled = true; } } </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM