如何在HTML和JavaScript中检查复选框是否为真
How do I check if a checkbox is true in HTML and JavaScript?
我正在制作一份表格,检查如果你过敏,可以吃什么。这是我的基本表单,但我需要检查是否选中了复选框。我试过了,但没用。变体和文本都是荷兰语,但你不必注意这一点。请帮我检查是否选中了复选框。非常感谢。
<!doctype html>
<html>
<head>
<title>Selecteer allergieën</title>
<h1>Selecteer je allergieën hieronder</h1>
</head>
<body>
<form>
<label for="pinda">
<input type="checkbox" id="pinda" value="Pinda's">Pinda's
</label><br>
<input type="button" value="Gaan ->" onClick="myFunction()">
</form>
<script>
function myFunction(){
var pinda = document.getElementById("pinda").checked;
if(pinda = checked){
alert("Je bent allergisch voor pinda's");
}
}
</body>
</html>
只剩下一个var,因此您可以轻松地查看代码。
您面临的问题是,您要检查输入是否已检查,这将返回布尔值(true
/false
),然后在if
中将未声明变量checked
的值分配给变量pinda
。你需要做的是:
function myFunction() {
var pinda = document.getElementById("pinda").checked;
if (pinda === true) {
alert("Je bent allergisch voor pinda's");
}
}
<form>
<label for="pinda">
<input type="checkbox" id="pinda" value="Pinda's" />Pinda's
</label>
<input type="button" value="Gaan ->" onClick="myFunction()" />
</form>
或者,更简单地说:
function myFunction() {
var pinda = document.getElementById("pinda").checked;
if (pinda) {
alert("Je bent allergisch voor pinda's");
}
}
<form>
<label for="pinda">
<input type="checkbox" id="pinda" value="Pinda's" />Pinda's
</label>
<input type="button" value="Gaan ->" onClick="myFunction()" />
</form>
顺便说一句,我建议将事件处理程序绑定在JavaScript中,而不是HTML本身(这允许不引人注目的JavaScript和更容易的长期维护):
function myFunction() {
var pinda = document.getElementById("pinda").checked;
if (pinda === true) {
alert("Je bent allergisch voor pinda's");
}
}
// using document.querySelector to retrieve the element from
// the document that matches the supplied CSS selector:
var button = document.querySelector('form input[type=button]');
// using addEventListener to bind myFunction as the
// click event-handler for the button node:
button.addEventListener('click', myFunction);
<form>
<label for="pinda">
<input type="checkbox" id="pinda" value="Pinda's" />Pinda's
</label>
<input type="button" value="Gaan ->" />
</form>
- 现在我知道你的问题标签需要像
</script>
一样结束 - 如果条件现在也是正确的,则应该是CCD_ 7
<!doctype html>
<html>
<head>
<title>Selecteer allergieën</title>
<h1>Selecteer je allergieën hieronder</h1>
</head>
<body>
<form>
<label for="pinda">
<input type="checkbox" id="pinda" value="Pinda's">Pinda's
</label>
<br>
<input type="button" value="Gaan ->" onClick="myFunction()">
</form>
<script>
function myFunction() {
var pinda = document.getElementById("pinda").checked;
if (pinda === true) {
alert("Je bent allergisch voor pinda's");
}
}
</script>
</body>
</html>
相关文章:
- JavaScript-切换“;全部检查”;复选框true/false
- 检查数据库中复选框的值
- JQuery:如何检查某个类的所有复选框是否都在表单中选中
- knockoutjs,复选框是有延迟的检查
- 关于复选框检查的困惑
- 复选框检查器在表中不起作用.代码在没有表标记的情况下工作
- 在复选框检查的更改功能上需要计算一个值
- 复选框检查事件验证没有 Javascript
- 我的JavaScript在复选框检查中有一些错误
- 如何使用angularjs根据复选框检查和取消选中来制作chckbox列表
- 如何使用AngularJS通过已检查的值进行复选框检查
- 根据复选框检查并设置url变量
- 如何在JavaScript/jQuery中弹出模态并运行复选框检查
- JavaScript复选框检查相关问题
- 如何使用输入复选框检查特定图像
- 复选框检查验证
- 数组复选框.检查javascript不工作
- 用于复选框检查的Java字符串到JavaScript字符串数组
- 复选框检查缓慢
- 我有麻烦得到复选框.检查工作