您的代码的问题在于您只是在第一次加载页面时设置了高、低、常见的值。这些值始终保持不变。
您应该在 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看看它的工作
可能这会对你有所帮助。