换算的结果出现一秒就返回原来的样子了,想请教这个问题要咋解决呀T-T
题目是:基于Java Web利用JavaScript编写一个金额找零的系统,用输入框输入一个整数,表示找零的数量,数值为1~100。假如系统中有50、20,10、5,1 这5种面额的纸币,显示每种纸币应该找的数量。
代码如下:
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<script>
function calculate(){
var amount = document.getElementById('amount').value;
var denominations = [50,20,10,5,1];
var count = [];
for (var i = 0; i < denominations.length; i++) {
count[i] = Math.floor(amount / denominations[i]);
amount %= denominations[i];
}
var result = document.getElementById('result');
result.innerHTML = "<h2>金额找零结果</h2>";
for (var i = 0; i < denominations.length; i++) {
if (count[i] > 0) {
result.innerHTML += denominations[i] + "元:" + count[i] + "张<br>";
}
}
}
</script>
<h1>金额找零系统</h1>
<form>
请输入要找零的金额(1~100):
<input type="text" id="amount">
<button onclick="calculate()">计算</button>
</form>
<div id="result"></div>
</body>
</html>
就是会从图1跳成图2,但是显示结果1秒就跳回图1了,不清楚是什么问题导致的
图1:

图2:
