我正在编写一个脚本,用于对表单中的文本输入进行计算。出于某种原因,我的计算在每次页面加载时只执行一次。当我按下“计算”按钮时,我必须刷新页面才能让函数再次运行。
这是我的按钮的 HTML:
<input type="button" value="Calculate" onclick="calculate(high, low, common);" />
<input type="reset" />
<div id="result"></div>
这是计算函数(变量在别处定义):
var calculate = function (high, low, common) {
if (high.length < 1 || low.length < 1 || common.length <1) {
document.getElementById('result').innerHTML="Please enter a number for all fields.";
} else {
if ((high - common) > (common - low)) {
document.getElementById('result').innerHTML="Result 1.";
} else if ((common - low) > (high - common)) {
document.getElementById('result').innerHTML="Result 2.";
} else if ((common - low) === (high - common)) {
document.getElementById('result').innerHTML="Result 3.";
}
}
};
我是否做了一些事情来阻止该函数在每次页面加载时运行多次?
最佳答案
您的代码存在问题,您只是在第一次加载页面时设置了高、低、常见。 这些值始终保持不变。
您应该在 onclick 事件中传递您的值。
更新了您的 - jsfiddle
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<meta charset = "UTF-8" />
</head>
<body>
<h1>Calculator</h1>
<!--Form forvalue inputs-->
<form>
<label for="highRi">Highest:</label><input type ="text" id="highRi" />
<label for="lowRi">Lowest:</label><input type="text" id="lowRi" />
<label for="comm">Common Point:</label><input type ="text" id="comm" />
<!--Clicking the button should run the calculate() function-->
<input type="button" value="Calculate" onclick="calculate(document.getElementById('highRi').value, document.getElementById('lowRi').value, document.getElementById('comm').value);" />
<input type="reset" />
<!--Div will populate with result after calculation is performed-->
<div id="result"></div>
</form>
<script type="text/javascript">
var calculate = function (high, low, common) {
if (high.length < 1 || low.length < 1 || common.length <1) {
document.getElementById('result').innerHTML="Please enter a number for all fields.";
} else {
if ((high - common) > (common - low)) {
document.getElementById('result').innerHTML="Result 1.";
} else if ((common - low) > (high - common)) {
document.getElementById('result').innerHTML="Result 2.";
} else if ((common - low) === (high - common)) {
document.getElementById('result').innerHTML="Result 3.";
}
}
};
</script>
</body>
</html>
或者您可以简单地在函数调用中获取这些元素值。
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<meta charset = "UTF-8" />
</head>
<body>
<h1>Calculator</h1>
<!--Form forvalue inputs-->
<form>
<label for="highRi">Highest:</label><input type ="text" id="highRi" />
<label for="lowRi">Lowest:</label><input type="text" id="lowRi" />
<label for="comm">Common Point:</label><input type ="text" id="comm" />
<!--Clicking the button should run the calculate() function-->
<input type="button" value="Calculate" onclick="calculate();" />
<input type="reset" />
<!--Div will populate with result after calculation is performed-->
<div id="result"></div>
</form>
<script type="text/javascript">
var calculate = function () {
var high = document.getElementById('highRi').value;
var low = document.getElementById('lowRi').value
var common = document.getElementById('comm').value
if (high.length < 1 || low.length < 1 || common.length <1) {
document.getElementById('result').innerHTML="Please enter a number for all fields.";
} else {
if ((high - common) > (common - low)) {
document.getElementById('result').innerHTML="Result 1.";
} else if ((common - low) > (high - common)) {
document.getElementById('result').innerHTML="Result 2.";
} else if ((common - low) === (high - common)) {
document.getElementById('result').innerHTML="Result 3.";
}
}
};
</script>
的 Jsfiddle See it working 的 也许这会对您有所帮助。
关于javascript - 为什么我的函数每次加载页面只运行一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11991749/