简体   繁体   English

简单的BMR计算器HTML JavaScript

[英]Simple BMR calculator HTML JavaScript

This is the beginning of my term paper. 这是我学期论文的开始。 What is wrong and why? 怎么了?为什么?

My calculator doesn't show the result. 我的计算器没有显示结果。

 <!DOCTYPE html> <html> <head> <script> var bmr; var age = document.getElementById("age").value; var gender = document.getElementById("gender").value; var height = document.getElementById("height").value; var weigth = document.getElementById("weigth").value; if (gender == "masc") { bmr = 66.5 + ( 13.75 * weigth ) + ( 5.003 * height ) – ( 6.755 * age ) } else { bmr = 655.1 + ( 9.563 * weigth ) + ( 1.850 * height ) – ( 4.676 * age ) } </script> </head> <body> <form action="#"> <input type="radio" id="gender" value="masc" checked> Male<br> <input type="radio" id="gender" value="fem"> Female<br> Age:<br> <input type="number" id="age" value="20"><br> Height:<br> <input type="number" id="height" value="180"><br> Weight:<br> <input type="number" id="weigth" value="80"><br> </form> <button type="button" onclick="document.getElementById('lblResult').innerHTML = bmr"> Result</button> <p id="lblResult">BMR</p> </body> </html> 

Like the comment mentioned you have a dash instead of a minus sign in your code preventing it from working. 就像提到的注释一样,您在代码中使用破折号而不是减号来阻止其工作。

bmr = 66.5 + ( 13.75 * weigth ) + ( 5.003 * height ) here-> - ( 6.755 * age )

You will also have to move your script to the end of the html file, just before the closing </body> tag beacause in your code when you select the elements they are not loaded so it dosen't work. 您还必须将脚本移动到html文件的末尾,因为在代码中选择未加载的元素时,由于在代码中</body>标记之前,因此脚本无法正常工作。

And the onclick event wont work the way you intended, its better if you make event inside the script. 并且onclick事件将无法按您预期的方式工作,如果您在脚本中创建事件,则效果会更好。

 document.getElementsByTagName("button")[0].addEventListener("click", 
 function() {
   calc();
   document.getElementById('lblResult').innerHTML = bmr;

 })

You sould also move the calculation code inside a function so you can calculate the values again, as it stands whenever you click result you will get the same value. 您还可以将计算代码移动到函数内部,以便可以再次计算值,因为单击结果时它将保持不变,因此您将获得相同的值。

Here is a working Example . 这是一个有效的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM