首页 > 解决方案 > 为什么代码末尾的分数变量没有显示?

问题描述

这是一个表格,它检查是否检查了某个收音机并在分数上加 5,但由于某种原因控制台一直说它没有定义。您可以通过填写第一页的信息进入第二页,然后您将找到电台测验。你必须选择 x 并且 score 会加 5,但由于某种原因它没有显示。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="yeah.css">
</head>
<body>
    <fieldset>
    <form id="form1">
        <h1>Formulaire</h1>
        <h2>Votre nom</h2>  <input type="text" name="" id="nom"><br>
        <p id="p1" ></p>
        <h2>Votre prenom</h2>  <input type="text" name="" id="prenom"><br>
        <p id="p2"></p>
        <h2>Class</h2> <select name="" id="class">
            <optgroup label="choose"  >
               
                <option value="info">info</option>
                <option value="tech">tech</option>
                <option value="lettre">lettre</option>
            </optgroup><br>
            <p id="p3"></p>


        </select>
        <h2>Groupe:</h2> <input type="number" id="num"><br>
        <button onclick="check()" type="button" >send</button>
    <button>reset</button>
    </form>
    </fieldset>
    
    
    <fieldset>
        <form action="" id="quiz">
            <h1></h1>
            <h2>what is the right variable name</h2>
            <input type="radio" name="first" id="first">
            x <br>
            <input type="radio" name="" id="">
            4gg <br>
            <input type="radio" name="" id="">
            if <br>
            <h2>check the correct loop syntax</h2>
            <input type="checkbox" name="" id="one">for (i++,0,9){}<br> 
            <input type="checkbox" name="" id="two">do {}while()<br>
            <input type="checkbox" name="" id="">for i in range(): <br>
            <br>
            <h2>How Find the lenght of A </h2>
            <textarea name="" id="" cols="30" rows="2"></textarea><br>
            <button  onclick="quiz()" >calculate result</button>
            <button>reset</button>

        </form>
    </fieldset>
    <script lang="javascript" >
        function check(){
            valid = true;
            a=document.getElementById("nom").value;
            b=document.getElementById("prenom").value;
            c=document.getElementById("class").value;
            d=document.getElementById("num").value;
            if (a==""){
                document.getElementById("p1").innerHTML="bruv type ur name"
                valid= false
            }
            if (valid == true){
                document.getElementById("form1").style.display="none"
                document.getElementById("quiz").style.display="block"
            }

        }
        
        function quiz(){
            const score= 0
            f=document.getElementById("first")
            if (f.checked == true ){
                
                score = 5;
                
                
                
            }
        }
        alert(score)
        
        

    </script>
</body>
</html>

标签: javascripthtml

解决方案


alert(score)在声明的函数之外调用score。这超出了范围。我很确定您打算将它包含在函数中。

此外,正如@seriously 指出的那样,声明为常量是没有意义score的,因为常量在声明后不能修改(您必须在示例中这样做)。相反,使用var.

function check() {
  valid = true;
  a = document.getElementById("nom").value;
  b = document.getElementById("prenom").value;
  c = document.getElementById("class").value;
  d = document.getElementById("num").value;
  if (a == "") {
    document.getElementById("p1").innerHTML = "bruv type ur name"
    valid = false
  }
  if (valid == true) {
    document.getElementById("form1").style.display = "none"
    document.getElementById("quiz").style.display = "block"
  }
}


function quiz() {
  var score = 0
  f = document.getElementById("first")
  if (f.checked == true) {
    score = 5;
  }
  alert(score)
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="yeah.css">
</head>

<body>
  <fieldset>
    <form id="form1">
      <h1>Formulaire</h1>
      <h2>Votre nom</h2> <input type="text" name="" id="nom"><br>
      <p id="p1"></p>
      <h2>Votre prenom</h2> <input type="text" name="" id="prenom"><br>
      <p id="p2"></p>
      <h2>Class</h2>
      <select name="" id="class">
        <optgroup label="choose">

          <option value="info">info</option>
          <option value="tech">tech</option>
          <option value="lettre">lettre</option>
        </optgroup><br>
        <p id="p3"></p>


      </select>
      <h2>Groupe:</h2> <input type="number" id="num"><br>
      <button onclick="check()" type="button">send</button>
      <button>reset</button>
    </form>
  </fieldset>


  <fieldset>
    <form action="" id="quiz">
      <h1></h1>
      <h2>what is the right variable name</h2>
      <input type="radio" name="first" id="first"> x <br>
      <input type="radio" name="" id=""> 4gg <br>
      <input type="radio" name="" id=""> if <br>
      <h2>check the correct loop syntax</h2>
      <input type="checkbox" name="" id="one">for (i++,0,9){}<br>
      <input type="checkbox" name="" id="two">do {}while()<br>
      <input type="checkbox" name="" id="">for i in range(): <br>
      <br>
      <h2>How Find the lenght of A </h2>
      <textarea name="" id="" cols="30" rows="2"></textarea><br>
      <button onclick="quiz()">calculate result</button>
      <button>reset</button>
    </form>
  </fieldset>
</body>

</html>


推荐阅读