我正在编写一个简单的函数来计算矩形的面积......
这是我的代码:
<form id="rectangleForm" method="post">
<h1>Calculate the area of a rectangle:</h1>
<div>
<label for="width">Width</label>
<input type="text" name="width" id="width" value="1.00" />
</div>
<div>
<label for="height">Height</label>
<input type="text" name="height" id="height" value="1.00"/>
</div>
<div>
<label for="area">Area</label>
<input type="text" name="area" id="rectarea" value="0.00" />
</div>
<div>
<input type="submit" value="Calculate" id="submit" />
</div>
</form>
<script>
function rectangle() {
'use strict';
var area;
var width = document.getElementById('width').value;
var height = document.getElementById('height').value;
total = width * height;
document.getElementById('area').value = total;
return false;
}
function init() {
'use strict';
var rectangleForm = document.getElementById('rectangleForm');
rectangleForm.onsubmit = rectangle();
}
window.onload = init();
</script>
我收到“未捕获的类型错误:无法读取 null 的属性‘值’。” var 宽度线上的错误。我不明白为什么。任何人都可以提供任何细节或解决方案吗?