首页 > 解决方案 > 使用javascript选中复选框时如何启用提交按钮?

问题描述

在用户选中复选框以表示他们已阅读并同意条款和条件之前,应禁用表单的提交按钮 这是复选框和提交按钮的代码 html 代码。我试过但失败了

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>

标签: javascripthtml

解决方案


在这里你弄错了:

你把你的复选框引用变量“提交”。然后,您使用了另一个名为“input”的变量,其中没有任何值。

更新答案:

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


推荐阅读