当我运行这个代码时,它只是说NaN,或者不是数字.我该怎么办才能解决这个问题



此外,我对代码和这个网站还很陌生。这是圆计算器的一个区域。圆面积的方程式是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>

相关内容

最新更新