我在尝试计算一个使用之前计算的数字的数字时遇到错误。
1st Calculation: Ending Depth - Starting Depth = Footage.
2nd Calculation: Footage / Rate of Penetration = Expected Hours to Drill.
第二次计算使用从第一次计算中导出的镜头。
我尝试了多种方法来做到这一点,包括添加另一个事件按钮,但我仍然收到一个似乎由 else{
有人知道这里发生了什么吗?
我有一个更长的代码,我已经尽可能地把它隔离了。
尝试创建另一个事件按钮,在这里完全迷失了。
function calculate() {
intervalFootage("Interval-1-StartMD", "Interval-1-EndMD", "Interval-1-Footage");
expectedHours("Interval-1-ROP", "Interval-1-Footage", "Interval-1-ExpectedHours");
}
function intervalFootage(startingDepth, endingDepth, output) {
var starting = document.getElementById(startingDepth).value;
var ending = document.getElementById(endingDepth).value;
if (starting > -1 && ending > -1) {
var answer = ending - starting;
} else {
var answer = "N/A";
}
document.getElementById(output).innerHTML = answer;
}
function expectedHours(rateofpenetration, intervallength, output) {
var rop = document.getElementById(rateofpenetration).value;
var length = document.getElementById(intervallength).value;
if (rop > 0 && length > 0) {
var hours = length / rop;
} else {
var hours = "N/A";
}
document.getElementById(output).innerHTML = hours;
}
<table style="width:100%">
<tr>
<th></th>
<th>Interval 1</th>
</tr>
<tr>
<td>Staring Depth (MD)</td>
<th><input type="number" id="Interval-1-StartMD" name="Interval-1- StartMD" min="0"></th>
</tr>
<tr>
<td>Ending Depth (MD)</td>
<th><input type="number" id="Interval-1-EndMD" name="Interval-1-EndMD" min="0"></th>
</tr>
<tr>
<td>Interval Footage (ft)</td>
<th id="Interval-1-Footage"></th>
</tr>
<tr>
<td>Expected Avg. ROP (ft/hr)</td>
<th><input type="number" id="Interval-1-ROP" name="Interval-1-ROP" min="0"></th>
</tr>
<tr>
<td>Expected Hours to Drill</td>
<th id="Interval-1-ExpectedHours"></th>
</tr>
</table>
<p></p>
<button onclick="calculate()">Calculate</button>
最佳答案
您似乎得到了错误的结果,因为这一行
var length = document.getElementById(intervallength).value;
实际上是在尝试获取 undefined
的 '' 元素的值。如果您试图获取之前插入到此 td 元素中的值,您应该像这样使用 innerHTML
或 innerText
:
var length = document.getElementById(intervallength).innerText;
关于javascript - JavaScript 计算出错(引用前面的计算),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57515610/