Javascript计算器结果闪烁 [英] Javascript calculator result flashes

查看:166
本文介绍了Javascript计算器结果闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:已编辑,在所有变量前添加了var,谢谢。我尝试删除< form>,但当它执行时,它不起作用。

My Code: "edited, added "var" in front of all variable, thanks. I tried removing the <form> but it wouldn't function when I did.

<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>

当我点击计算按钮时,结果显示在div中,但只显示一秒钟然后消失。请记住,我对JavaScript很陌生。任何帮助表示感谢。

When I click the "Calculate" button the result displays in the div, but only for a split second then disappears. Keep in mind I'm very new to JavaScript. Any help appreciated, thanks.

推荐答案

您正在提交表单,因此页面正在刷新。

You are submitting the form and so the page is refreshing.

返回false; 添加到 calculate 函数的末尾以防止提交表单。

Add return false; to the end of your calculate function to prevent the form submission.

这篇关于Javascript计算器结果闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆