簡體   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