简体   繁体   English

当我运行此代码时,它只显示 NaN 或不是数字。 我能做些什么来解决这个问题?

[英]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.

相关问题 这段代码有什么问题? 当我运行它的结果只是显示0 - what is wrong in this code? when i run it just shows 0 as the result 我该如何修复这个 NaN - How do i Fix this NaN 当我使用forEach方法时,浏览器会说没有这种功能。 会是什么呢? - When I use the forEach method, the browser says that there is no such function; what could it be? 我想对[数字集]使用image_tag而不只是一个数字。 (红宝石) - I want to use image_tag for the [ number set ] not just one number. (Ruby) 当用户关闭选项卡时,我运行什么 JavaScript 代码? - What JavaScript code do I run when the user closes the tab? 如何运行javascript以还原嵌入式脚本资源中的某些代码? - How do I run javascript to revert what some code in an embedded script resource just did? 为什么 JavaScript 中是“NaN”,我该如何解决? - Why is'NaN' in JavaScript and how do I fix it? 当我运行动画功能时,什么也没有发生,该怎么办才能解决此问题? - When I run the animate function nothing happens, what can I do to fix this? typeof 给了我一个数字,但是相同变量的 console.log 给了我 NAN 我该如何解决这个问题 - typeof gives me a number but console.log of the same variable gives me NAN how do i fix this 运行此脚本时出现错误代码401,该怎么办? - I get the Error code 401 when I run this script, what can I do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM