javascript - document.getElementById("box5"&&"box14") 仅获取第二个参数
问题描述
我有一个脚本,将变量设置trouve_coupable
为true
if bothbox 5
并且box 14
已在 HTML 页面上进行检查。但无论我在页面上做什么,只要box 14
( ="case14"
) 是checked
,它就会返回,true
但它应该是false
。
(抱歉我的程序是法语的)
function verifier() {
if (document.getElementById("case5"&&"case14").checked) {
trouve_coupable = true;
}
else {
trouve_coupable = false;
}
if (trouve_coupable){
window.location="lieu16.html"
}
else {
alert("Incorrect! Faîtes plus attention aux détails qui vous entourent et prenez des notes!!");
window.location="lieu17r.html"
}
}
这是 HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Poursuite dans la forêt</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="script.js"></script>
</head>
<body>
<h1>Quel est le mot de passe?</h1>
Le nombre (Tous les indices ensembles)<br>
<input type="radio" name="coupable" id="case1" />01246
<input type="radio" name="coupable" id="case2" />10264
<input type="radio" name="coupable" id="case3" />46201
<input type="radio" name="coupable" id="case4" />26410
<input type="radio" name="coupable" id="case5" />62140
<input type="radio" name="coupable" id="case6" />41260
<br/>
Le mot<br>
<input type="radio" name="mobile" id="case7" />Hamburger
<input type="radio" name="mobile" id="case8" />Pringles
<input type="radio" name="mobile" id="case9" />Faygo
<input type="radio" name="mobile" id="case10" />Jambon cru
<input type="radio" name="mobile" id="case11" />Jambon cuit
<input type="radio" name="mobile" id="case12" />Jambon fumé
<input type="radio" name="mobile" id="case13" />Jambon de Bayonne
<input type="radio" name="mobile" id="case14" />Jambon
<input type="radio" name="mobile" id="case15" />Jambon Serrano
<input type="radio" name="mobile" id="case16" />Jambon Ibérique
<input type="radio" name="mobile" id="case17" />Jambon Bodega
<input type="radio" name="mobile" id="case18" />Lardons
<input type="radio" name="mobile" id="case19" />Bacon
<br/>
<input type="button" value = "Envoyer" onclick="verifier()">
</body>
</html>
解决方案
您正在尝试将公式传递给,getElementById
但它应该接收一个元素的 id。你想要的是评估一个带有检查属性结果的公式。当您a && b
在 javascript 中执行时,它等同于a ? a : b
,因此您的 id "case5" && "case14" 是返回 case14 的常量表达式。解决这个问题,这就是你所拥有的。
function verifier() {
if (document.getElementById("case5").checked &&
document.getElementById("case14").checked) {
trouve_coupable = true;
}
else {
trouve_coupable = false;
}
if (trouve_coupable){
// window.location="lieu16.html"
alert("bon travail!")
}
else {
alert("Incorrect! Faîtes plus attention aux détails qui vous entourent et prenez des notes!!");
// window.location="lieu17r.html"
}
}
<h1>Quel est le mot de passe?</h1>
Le nombre (Tous les indices ensembles)<br>
<input type="radio" name="coupable" id="case1" />01246
<input type="radio" name="coupable" id="case2" />10264
<input type="radio" name="coupable" id="case3" />46201
<input type="radio" name="coupable" id="case4" />26410
<input type="radio" name="coupable" id="case5" />62140
<input type="radio" name="coupable" id="case6" />41260
<br/>
Le mot<br>
<input type="radio" name="mobile" id="case7" />Hamburger
<input type="radio" name="mobile" id="case8" />Pringles
<input type="radio" name="mobile" id="case9" />Faygo
<input type="radio" name="mobile" id="case10" />Jambon cru
<input type="radio" name="mobile" id="case11" />Jambon cuit
<input type="radio" name="mobile" id="case12" />Jambon fumé
<input type="radio" name="mobile" id="case13" />Jambon de Bayonne
<input type="radio" name="mobile" id="case14" />Jambon
<input type="radio" name="mobile" id="case15" />Jambon Serrano
<input type="radio" name="mobile" id="case16" />Jambon Ibérique
<input type="radio" name="mobile" id="case17" />Jambon Bodega
<input type="radio" name="mobile" id="case18" />Lardons
<input type="radio" name="mobile" id="case19" />Bacon
<br/>
<input type="button" value = "Envoyer" onclick="verifier()">
推荐阅读
- asp.net - ERR_HTTP2_PROTOCOL_ERROR
- scala - Scala - 获取具有条件和 groupBy 的列?
- java - seek() 方法不适用于 java 中的 InputStream available() 方法
- c - 如何使用scanf处理无限循环的错误输入?
- python - 在父类中使用super是什么意思?
- javascript - 检查字母表中的字母被用作对象数组中键的起始字母的次数
- unit-testing - 单元测试如何等待 JavaFX 平台的关闭?
- excel - 自定义 VBA 函数引发意外输出
- python - 为什么此异常打印为“列表索引超出范围”?
- python - tweepy.error.TweepError: [{'code': 50, 'message': 'User not found.'}]