Javascript 单击“提交按钮”后,文本迅速消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11285412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
text disappears rapidly after click on 'submit-button'
提问by user114158
Got a problem. this is my code:
有问题。这是我的代码:
<html>
<body>
<form>
<p>13-7: <input id="in1" type="text"" /><input type="submit" onclick="check(6, 'in1', 'out1')" value="Tjek!"/></p>
<p id="out1"></p>
<p>20-7: <input id="in2" type="text"" /><input type="submit" onclick="check(13, 'in2', 'out2')" value="Tjek!" /></p>
<p id="out2"></p>
<script type="text/javascript">
function check(facit, input, output) {
var answer, evaluation;
answer = document.getElementById(input).value;
evaluation = (answer == facit) ? "Correct" : "Wrong";
document.getElementById(output).innerHTML = evaluation;
return false;
}
</script>
</form>
</body>
</html>
When I click the submit-button, the 'correct/wrong' shows only for a moment. I want it to stay on the site, any advice?
当我单击提交按钮时,“正确/错误”仅显示片刻。我希望它留在网站上,有什么建议吗?
回答by Philippe Leybaert
The submit buttons are submitting your form (so reloading your page).
提交按钮正在提交您的表单(因此请重新加载您的页面)。
Change the buttons to type="button"
.
将按钮更改为type="button"
。
回答by Ry-
Your buttons are submit buttons (<input type="submit">
) so they will submit the form and refresh the page on each click. Change your buttons to <input type="button">
instead.
您的按钮是提交按钮 ( <input type="submit">
),因此它们将在每次单击时提交表单并刷新页面。将您的按钮<input type="button">
改为。
回答by Praveen Kumar Purushothaman
Change the onclick function from:
将 onclick 函数从:
onclick="check(13, 'in2', 'out2')"
to:
到:
onclick="return check(13, 'in2', 'out2')"
Alternatively, if you don't want the form to be submitted, as you have two Submit buttons, it is better to use:
或者,如果您不想提交表单,因为您有两个提交按钮,最好使用:
<input type="button" />
回答by Siva Charan
回答by Hogan
The form is being submitted and returned with the original page.
表单正在提交并与原始页面一起返回。
What do you want to do?
你想让我做什么?
I dont want it returned with the original page .. I want it to say 'correct' og 'wrong' below the input
If you don't want to submit the data then do as minitech suggests, change the type from submit to button.
如果您不想提交数据,请按照 minitech 的建议进行操作,将类型从提交更改为按钮。
If you want to save the data you will need to write server side code and then create html with those values filled in (on the server side.)
如果要保存数据,则需要编写服务器端代码,然后创建填充这些值的 html(在服务器端。)