我做了一個html文檔,計算在我的html文檔的高度,寬度和長度的輸入框中輸入的任何東西的表面積和體積。我不想使用按鈕來運行函數,而是在指定爲表面區域和體積的輸入框中顯示答案,我希望使用onload並在每個輸入框被更改時都使用這兩個函數。我對Javascript很陌生,下面我試過的方法只是將屏幕留空,究竟是什麼導致答案不能顯示在文本框中?我該如何解決這個問題?onChange不能在我的onLoad函數中顯示正確的答案
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Surface Area & Volume</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<script>
"use strict";
var $ = function(id) {
return document.getElementById(id);
};
var amountVolume = 0;
var length = $("length");
var width = $("widthData");
var height = $("heightData");
var surfaceArea;
var surfaceAreaArray = [];
function output() {
function calculateVolume(length, width, height) {
amountVolume = parseFloat(length) * parseFloat(width) * parseFloat(height);
var volumeRound = amountVolume.toFixed(2);
$('volume').value = volumeRound;
}
function calculateSurfaceArea(length, width, height) {
surfaceAreaArray[0] = parseFloat(length) * parseFloat(width) * 2;
surfaceAreaArray[1] = parseFloat(length) * parseFloat(height) * 2;
surfaceAreaArray[2] = parseFloat(height) * parseFloat(width) * 2;
surfaceArea = surfaceAreaArray[0] + surfaceAreaArray[1] + surfaceAreaArray[2];
var areaRound = surfaceArea.toFixed(1);
$('area').value = areaRound;
}
}
window.onload = function() {
$("length").onchange = output;
$("widthData").onchange = output;
$("heightData").onchange = output;
}
</script>
<div id="wrapper">
<form>
<h1>Surface Area & Volume</h1>
<p>Length:
<input type="text" id="length" value="0"/>
</p>
<br />
<p>Width:
<input type="text" id="widthData" value="0"/>
</p>
<br />
<p>Height:
<input type="text" id="heightData" value="0"/>
</p>
<br />
<p class="change">Surface Area:
<input type="text" id="area" disabled="disabled" />
</p>
<br />
<p class="volume">Volume:
<input type="text" id="volume" disabled="disabled" />
</p>
<br />
</form>
</div>
</body>
</html>