我正在做一个脚本,点击时,首先检查生成的数字是否在范围内,然后,如果在该范围内,则生成某个复选框,我遇到的问题是点击后,或者只生成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>