此外,我对代码和这个网站还很陌生。这是圆计算器的一个区域。圆面积的方程式是3.14*r*r.
<input type="Number" id="index_radius">
<br><button onclick="myfunction()">Calculate</button>
<script>
function myfunction() {
var radius = document.getElementById("index_radius");
var calculate = radius * radius * 3.14;
console.log(calculate)
}
问题是radius
变量等于html元素,而不是实际值。要获得数字形式的值,可以添加.valueAsNumber
,以下代码有效。
function myfunction() {
var radius = document.getElementById("index_radius").valueAsNumber;
var calculate = radius * radius * Math.PI;
console.log(calculate)
}
<input type="Number" id="index_radius">
<br><button onclick="myfunction()">Calculate</button>