繁体   English   中英

Javascript函数对innerHTML没有做任何事情

[英]Javascript function not doing anything with innerHTML

因此,我编写了以下代码来计算 BMI。 我面临的问题是我的 javascript 函数不起作用。 我什至尝试插入一个示例文本(在代码中注释)以使用 javascript 进行测试,但这不起作用。 有人可以帮我找出问题吗?

 <h2>Welcome to BMI calculator</h2> <h4>Please fill in the required data.</h4> <form name="bmiform"> <fieldset style="width:280px;"> <legend> BMI CALCULATOR </legend> Name: <input type="text" value="" name="name"> <br> <br>Height: <input type="text" value="" name="height">Meters <br> <br>Weight: <input type="text" value="" name="weight">Kilograms <br> <br> <input type="button" value="Calculate" onclick="BMIcalc(document.bmiform.weight.value,document.bmiform.height.value)"> </fieldset> </form> <div> <br> <br>Your BMI is: <b id="samples"></b> <!--Inner HTML does not inserts text in here--> <script> document.getElementById("samples").innerHTML = "Testing purposes"; //This code does not work. function BMIcalc(parseFloat(weight), parseFloat(height)) { var bmi = (weight / (height * height)); document.getElementById("samples").innerHTML = bmi; } </script> <br> <br> <table border="1" width="300px"> <tr> <th> BMI </th> <th> Weight Status </th> </tr> <tr> <td> Below 18.5 </td> <td> Underweight </td> </tr> <tr> <td> 18.5-24.9 </td> <td> Normal </td> </tr> <tr> <td> 25.0-29.9 </td> <td> Overweight </td> </tr> <tr> <td> 30.0 and above </td> <td> Obese </td> </tr> </table> </div>

你不能在你的函数声明中做像这个function BMIcalc(parseFloat(weight), parseFloat(height))这样的事情。

它必须是function BMIcalc(weight, height)并且你的 parsefloat 应该在你的函数体内或函数调用中。

按如下方式编辑您的函数,应在函数内调用parseFloat ,您不能像正在执行的那样在函数声明参数中调用函数:

function BMIcalc(weight, height) {
  var bmi = (parseFloat(weight) / (parseFloat(height) * parseFloat(height)));
  document.getElementById("samples").innerHTML = bmi;
}

编辑: 1. 我不喜欢解析任何值到onclick ,所以我将检索移动到函数BMIcalc 2.你不能对你的参数字段做任何操作,你必须在函数中解析或做其他事情。

 document.getElementById("samples").innerHTML = "Testing purposes"; //This code does not work. function BMIcalc() { var weight = parseFloat(document.bmiform.weight.value); var height = parseFloat(document.bmiform.height.value); var bmi = (weight / (height * height)); document.getElementById("samples").innerHTML = bmi; }
 <h2>Welcome to BMI calculator</h2> <h4>Please fill in the required data.</h4> <form name="bmiform"> <fieldset style="width:280px;"> <legend> BMI CALCULATOR </legend> Name: <input type="text" value="" name="name"> <br> <br>Height: <input type="text" value="" name="height">Meters <br> <br>Weight: <input type="text" value="" name="weight">Kilograms <br> <br> <input type="button" value="Calculate" onclick="BMIcalc()"> </fieldset> </form> <div> <br> <br>Your BMI is: <b id="samples"></b> <!--Inner HTML does not inserts text in here--> <br> <br> <table border="1" width="300px"> <tr> <th> BMI </th> <th> Weight Status </th> </tr> <tr> <td> Below 18.5 </td> <td> Underweight </td> </tr> <tr> <td> 18.5-24.9 </td> <td> Normal </td> </tr> <tr> <td> 25.0-29.9 </td> <td> Overweight </td> </tr> <tr> <td> 30.0 and above </td> <td> Obese </td> </tr> </table> </div>

暂无
暂无

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

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