我希望加热答案出现在加热表面 (mm) 旁边,但我无法让它工作。我只从 chrome 控制台收到以下错误消息
Uncaught TypeError: Cannot read property 'value' of null
我知道其他一切都有效,因为我添加了一个警告框,但我需要 innerHTML 才能工作。
这是html:
<html>
<head>
<script type="text/javascript" src="pipeheaterfunction.js">
</script>
</head>
<body>
<table>
<tr><td>Inner Diameter (mm):</td>
<td><input id="dia" onkeypress="pipeheater();"></td>
<tr><td>Pipe Thickness (mm):</td>
<td><input id="thi" onkeypress="pipeheater();"></td>
<tr><th>Calculate heater:</th>
<td><button onclick="pipeheater();">Calculate</button></td></tr>
<tr><td>Heating Surface(mm):</td>
<td><span class="output" id="surface"></span></td></tr>
</table>
</body>
</html>
这是javascript代码:
function pipeheater(){ //dia is the inner diameter of a pipe, thi is the pipe thickness
var dia = document.getElementById("dia").value;
var thi = document.getElementById("thi").value;
var hsur; //hsur is the heating surface required for a certain pipe in mm
hsur = 5*Math.sqrt(((dia-thi)*thi)/2);
var surface = hsur;
if(surface>0){
surface.innerHTML=surface.toFixed(1);
alert(surface.toFixed(1));
}
}
window.onload=pipeheater();