简体   繁体   English

为什么我的 JavaScript BMI 计算器不工作? 找不到错误

[英]Why is my JavaScript BMI Calculator not working? Cannot find the error

I have been for a few days now trying to program a basic BMI calculator, but something seems to be wrong.几天来,我一直在尝试编写基本的 BMI 计算器,但似乎出了点问题。 Tried the console, read a thousand posts here but cannot find my error.尝试了控制台,在这里阅读了一千篇文章,但找不到我的错误。 It just doesn't show any result.它只是没有显示任何结果。 I would really appreciate your help.我将衷心感谢您的帮助。

This is my HTML:这是我的 HTML:

 function printImc() { var peso = parseFloat(document.getElementById("peso").value); var altura = parseFloat(document.getElementById("altura").value); var imc = peso / (altura * altura).toFixed(2); document.getElementById("h2").innerHTML = imc; var indice = ""; if (imc <= 18.5) { indice = "Tienes bajo peso"; } else if (imc > 18.6 && imc <= 24.9) { indice = "Tienes un peso saludable"; } else if (imc > 25) { indice = "Tienes sobrepeso"; } document.getElementById("resultado").innerHTML = indice; } document.getElementById("button").addEventListener(onclick, printImc);
 <div id="container"> <form id="form"> <label for="peso">Peso<input type="text" id="peso" class="data" placeholder="60 kg"></label> <label for="altura">Altura<input type="text" id="altura" class="data" placeholder="1.70 mts"></label> <button id="button">Ok</button> </form> <div id="indice"> <h3>Tu índice de masa corporal es:</h3> <h2 id="h2">0</h2> </div> <div id="resultado"> </div> </div> <script type="text/javascript" src="calculadoramasacorporal.js"></script>

Another way to fix it would be to change the form to a div (maybe a good idea as I think forms are more for submitting information to an api etc..)修复它的另一种方法是将表单更改为 div(可能是个好主意,因为我认为表单更多用于向 api 等提交信息。)

Also I changed the .innrerHtml to .innerText as you only want to pass text to the element rather than more html elements此外,我将 .innrerHtml 更改为 .innerText 因为您只想将文本传递给元素而不是更多的 html 元素

 function printImc() { var peso = document.getElementById('peso').value var altura = document.getElementById('altura').value var imc = (peso / (altura * altura)).toFixed(2) console.log(imc) var indice = "" if (imc <= 18.5) { indice = "Tienes bajo peso" } else if (imc > 18.6 && imc <= 24.9) { indice = "Tienes un peso saludable" } else if (imc > 25) { indice = "Tienes sobrepeso" } document.getElementById('h2').innerText = imc document.getElementById('resultado').innerText = indice } document.getElementById('button').addEventListener('click', printImc)
 <div id="container"> <div id="form"> <label for="peso">Peso<input type="text" id="peso" class="data" placeholder="60 kg"></label> <label for="altura">Altura<input type="text" id="altura" class="data" placeholder="1.70 mts"></label> <button id="button">Ok</button> </div> <div id="indice"> <h3>Tu índice de masa corporal es:</h3> <h2 id="h2">0</h2> </div> <div id="resultado"> </div> </div>

The button submits the form, which as a result reloads the page (it's a quite irritating default if you ask me).该按钮提交表单,结果会重新加载页面(如果您问我,这是一个非常令人讨厌的默认设置)。 You need to manually prevent this by adding function printImc(event) { event.preventDefault();您需要通过添加function printImc(event) { event.preventDefault();来手动防止这种情况function printImc(event) { event.preventDefault();

As pointed out by @NishantSingh, another solution is to define type="button" on the button to prevent reloading the page (I didn't know that).正如@NishantSingh 所指出的,另一种解决方案是在type="button"上定义type="button"以防止重新加载页面(我不知道)。

Second error, .addEventListener(onclick) should be .addEventListener("click") .第二个错误, .addEventListener(onclick)应该是.addEventListener("click")

Third error (more like a warning because it works nonetheless) : as pointed out by @jkoestinger, .tofixed() returns a string (!).第三个错误(更像是警告,因为它仍然有效):正如@jkoestinger 所指出的, .tofixed()返回一个字符串 (!)。 (altura * altura).toFixed(2) is a string, and then you divide a number with a string, which shouldn't work... but still does, probably because JS automatically casts "26.8" to 26.8 . (altura * altura).toFixed(2)是一个字符串,然后你用一个字符串除以一个数字,这不应该工作......但仍然可以,可能是因为JS自动将"26.8"转换为26.8

 function printImc() { var peso = parseFloat(document.getElementById("peso").value); var altura = parseFloat(document.getElementById("altura").value); var imc = (peso / (altura * altura)).toFixed(2); document.getElementById("h2").innerHTML = imc; var indice = ""; if (imc <= 18.5) { indice = "Tienes bajo peso"; } else if (imc > 18.6 && imc <= 24.9) { indice = "Tienes un peso saludable"; } else { indice = "Tienes sobrepeso"; } document.getElementById("resultado").innerHTML = indice; } document.getElementById("button").addEventListener("click", printImc);
 <div id="container"> <form id="form"> <label for="peso">Peso<input type="text" id="peso" class="data" placeholder="60 kg"></label> <label for="altura">Altura<input type="text" id="altura" class="data" placeholder="1.70 mts"></label> <button id="button" type="button">Ok</button> </form> <div id="indice"> <h3>Tu índice de masa corporal es:</h3> <h2 id="h2">0</h2> </div> <div id="resultado"> </div> </div> <script type="text/javascript" src="calculadoramasacorporal.js"></script>

Below is your solution:以下是您的解决方案:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Calculadora de masa corporal</title> </head> <body> <div id="container"> <form id="form"> <label for="peso">Peso<input type="text" id="peso" class="data" placeholder="60 kg"></label> <label for="altura">Altura<input type="text" id="altura" class="data" placeholder="1.70 mts"></label> <button id="button" type="button">Ok</button> </form> <div id="indice"> <h3>Tu índice de masa corporal es:</h3> <h2 id="h2">0</h2> </div> <div id="resultado"> </div> </div> <script type="text/javascript"> function printImc(){ console.log("hello") var peso = parseFloat(document.getElementById("peso").value); var altura = parseFloat(document.getElementById("altura").value); var imc = peso / (altura * altura).toFixed(2); document.getElementById("h2").innerHTML = imc; var indice = ""; if (imc <= 18.5){ indice = "Tienes bajo peso"; } else if (imc > 18.6 && imc <= 24.9) { indice = "Tienes un peso saludable"; } else if (imc > 25) { indice = "Tienes sobrepeso"; } document.getElementById("resultado").innerHTML = indice; } document.getElementById("button").addEventListener("click", printImc); </script> </body> </html>

Your code is good, a few mistakes are there like incorrect event listener, as well as the button type, is missing.你的代码很好,有一些错误,比如不正确的事件监听器,以及按钮类型丢失。

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

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