javascript - 使用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>
解决方案
在这里你弄错了:
你把你的复选框引用变量“提交”。然后,您使用了另一个名为“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>
推荐阅读
- javascript - 使用 lodash 按值对对象进行排序
- windows - RNDIS 驱动程序在 Win 10 Pro 下崩溃,但在 Win 10 家庭/学生版上不崩溃
- c# - 如何在 C# 中使用 HTTPContext 获取网址
- javascript - 使用 onclick="location... 代替 href。为什么我应该避免使用它和替代选项
- sql-server - 从 Microsoft SQL Server Management Studio 下载 SQL 数据库?
- django - 在 django 中自动填充数据库中的字段
- android - 用于注释模型的 Moshi 适配器
- node.js - 删除数据库和创建同名数据库问题
- java - 从 IntelliJ 中的 .iml 生成 pom.xml
- amazon-web-services - stopDBCluster (Aurora) 不是函数 aws-sdk javascript lambda