我想显示一个 div(取决于输入的值),但我在控制台中没有看到任何错误指向正确的方向,但我的代码有问题:
<style type="text/css">
#outputOne{display: none;}
#outputTwo{display: none;}
#outputThree{display: none;}
</style>
</head>
<body>
<form method="POST" onsubmit="return showResults()">
<input type="text" id="valueOne" /><br />
<input type="text" id="valueTwo" /><br />
<input type="text" id="valueThree" /><br />
<input type="submit" id="submit" />
</form>
<div id="outputOne">Value One</div>
<div id="outputTwo">Value Two</div>
<div id="outputThree">Value Three</div>
<script>
function showResults(){
if(value <= 100){
document.getElementById('outputOne').style.display = "block";
return true;
} if(value > 500){
document.getElementById('outputTwo').style.display = "block";
return true;
}if(value > 10000){
document.getElementById('outputThree').style.display = "block";
return true;
}
}
</script>
最佳答案
如果你想看到一些东西,你应该通过返回 false 而不是 true 来阻止提交,因为现在表单将在所有情况下提交,你永远不会看到显示的 div,因为页面将被刷新。
希望这对您有所帮助。
关于javascript - 提交时显示 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35689021/