简体   繁体   English

Javascript计算器结果闪烁

[英]Javascript calculator result flashes

My Code: "edited, added "var" in front of all variable, thanks. I tried removing the <form> but it wouldn't function when I did. 我的代码:“已编辑,在所有变量之前添加了“ var”,谢谢。我尝试删除了<form>,但在执行该操作时将无法运行。

<head>
<title>Calculator</title>
<script language="javascript">
function calculate()
{
var v=parseInt(document.forms[0].txt1st.value);
var w=parseInt(document.forms[0].txt2nd.value);
var x=parseInt(document.forms[0].txt3rd.value);
var y=703;
var z = (v/w/x*y).toFixed(3);
document.getElementById("display").innerHTML=z;
}

</script>
</head>
<body>
<form name="cal" method="post" action="">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input name="txt1st" type="text" id="txt1st"></td>
<td><input name="txt2nd" type="text" id="txt2nd"></td>
<td><input name="txt3rd" type="text" id="txt3rd"></td>
<td>x 703</td>
</tr>
</table>
<button onclick="calculate()">Calculate</button>
<div id="display"></div>
</body>

When I click the "Calculate" button the result displays in the div, but only for a split second then disappears. 当我单击“计算”按钮时,结果将显示在div中,但只有一瞬间,然后消失。 Keep in mind I'm very new to JavaScript. 请记住,我是JavaScript的新手。 Any help appreciated, thanks. 任何帮助表示赞赏,谢谢。

You are submitting the form and so the page is refreshing. 您正在提交表单,因此页面正在刷新。

Add return false; 添加return false; to the end of your calculate function to prevent the form submission. 到您的calculate函数的末尾以防止表单提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM