5

我正在编写一个脚本,该脚本使用表单中的文本输入进行计算。出于某种原因,我的计算在每次页面加载时只执行一次。当我按下“计算”按钮时,我必须刷新页面才能再次运行该功能。

这是我的按钮的 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.";
        }
      }
   }; 

我是否正在做一些事情来阻止该功能在每个页面加载时运行多次?

4

2 回答 2

1

您的代码的问题在于您只是在第一次加载页面时设置了高、低、常见的值。这些值始终保持不变。

您应该在 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看看它的工作 ​​可能这会对你有所帮助。

于 2012-08-17T05:24:41.840 回答
0

对我来说,如果我将值更改为高、低和通用,您的代码也可以工作,试试这个方法,它可能对你有用

<input type="button" id="button" value="Calculate" />

var button = document.getElementById("button");
var high = document.getElementById("high").value;
var low = document.getElementById("low").value;
var common = document.getElementById("common").value;
button.addEventListener(onclick, calculate(high, low, common));
于 2012-08-16T16:52:40.967 回答