[英]When I run this code it just says NaN or not a number. What could I do to fix this?
Also I'm really new to code, and this website.另外,我对代码和这个网站真的很陌生。 This is an area of a circle calculator.这是一个圆计算器的面积。 The equation for area of a circle is 3.14 * r * r.圆的面积方程是 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)
}
The problem is that the radius
variable is equal to the html element, not the actual value.问题是radius
变量等于html元素,而不是实际值。 To get the value as a number, you can addd .valueAsNumber
the following code works.要以数字形式获取值,您可以添加.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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.