0

我正在做一个脚本,点击时,首先检查生成的数字是否在范围内,然后,如果在该范围内,则生成某个复选框,我遇到的问题是点击后,或者只生成1项目或不显示任何内容

这是我现在测试的方式

       var count=30;
    var counter=setInterval(timer, 1000);
    function timer(){
      count=count-1;
        if(count <= 0){
          clearInterval(counter);
          return;
          checker();
        }

 document.getElementById("timer").innerHTML=count + " secs"; // watch for spelling
}

    function start(){
      var timeLeft = 30;
      var x = document.getElementById("invisible");
      var matriu = document.getElementById("Matriu").value;
      var btn = document.createElement("INPUT");
      btn.type = "checkbox";
      btn.id = "test";
      if(matriu >= 1 && matriu <= 10){
        if (x.style.display === "block") {
          timer();
          x.style.display = "none";
      } else {
          x.style.display = "block";
      }
      } else {
        alert("Error, numero incorrecte");
      }   alert(matriu);
      for ( i=0; i <= matriu; i++) {
        document.getElementById("invisible").appendChild(btn);
      }
    }
    function checker() {
      for (z = 1; z<3; z++) {
  document.getElementById('test'+z).checked = true;
}
   };
#invisible{
  display: none;
}
 

   <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Exercici 1</title>
      </head>
      <body>
            Matriu:<input type="text" id="Matriu">
   <input type="button" onclick="start()" value="começa">
  <div id="invisible"> 
    <h3>Nivell X</h3> 
    <span id="timer"></span>
    <br>
  </div>
      </body>
    </html>

4

1 回答 1

0

我建议您进行一些清理工作,但要让它按照您的要求进行操作,请尝试以下操作:

function start() {
    var x = document.getElementById("invisible");
    var matriu = document.getElementById("Matriu").value;

    if (matriu >= 1 && matriu <= 10) {
      x.style.display = x.style.display === "block" ? "none" : "block";
    } else {
      alert("Error, numero incorrecte");
    }   
    alert(matriu);

    for (var i = 0; i < matriu; i++) {
      var btn = document.createElement("input");
      document.body.appendChild(btn);
    }

}
于 2018-01-26T18:48:41.250 回答