【发布时间】:2018-03-17 16:48:46
【问题描述】:
我有一个小问题。为什么我必须在我的 js 脚本运行之前单击我的提交按钮两次?我希望当我单击按钮时发送数据,并且 js 脚本运行而无需再次单击 这是我的页面:
<html>
<head>
<script src="js-scripts.js"></script>
<style>
.content {
max-width: 600px;
margin: auto;
}
.test{
width:200px;
text-align: center;
}
</style>
</head>
<body>
<div class="content" id="tot-op">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<center>
<div class="content" id="tot-op">
<fieldset>
<input type="number" min="1" max="7" name="num" required>
<br><br>
<input type="submit" value="Invia" onclick="hide()">
</fieldset>
</div>
</center>
</form>
</div>
<div id="insert" style="display:none">
<?php
$test=$_POST["num"];
echo $test;
?>
</div>
</body>
</html>
这是我的脚本:
function hide(){
document.getElementById("tot-op").style.display = "none";
document.getElementById("insert").style.display = "block";
}
我能做什么?
【问题讨论】:
-
当你第一次点击时,表单被提交了,对吧?然后你再次点击,js脚本运行?
-
@MathewsMathai 是的。它的功能就像你说的那样
-
那是因为您第一次填写表格然后
submitaction 有效,因为您已经满足了所有要求,第二次您可能没有填写表格就点击了按钮。因此,提交不会发生,但会触发onclick。在第二次点击之前再次填写表格并检查会发生什么。 -
@MathewsMathai 好像是第一次
-
我没听懂你。您是否填写了两次点击的表单?
标签: javascript php html css linux