首页 > 解决方案 > document.getElementById("box5"&&"box14") 仅获取第二个参数

问题描述

我有一个脚本,将变量设置trouve_coupabletrueif 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>

标签: javascripthtml

解决方案


您正在尝试将公式传递给,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()">


推荐阅读