BMI计算器显示NaN而不是值

时间:2019-03-18 07:54:31

标签: javascript html html5

我要制造我的计算器...在此计算器中,单击按钮,然后输入我的计算器,计算出您的BMI。

function cal() {
  var h = document.getElementsByName("height").value; //it's get my height
  var w = document.getElementsByName("weight").value; //it's get my weight
  var r = (w / (h * h)) * 10000; //it's calculate MBI
  document.getElementById("calcu").innerHTML = r; //the answer shows under my form
  alert(r);
}
<form>
  <fieldset>
    <legend>محاسبه BMI</legend>//it's calculate BMI قد (cm)<br>//it's height
    <input type="number" name="height" placeholder="175" value="175"><br> وزن (kg)<br>//it's weight
    <input type="number" name="weight" placeholder="75" value="73"><br> جنسیت
    <br>//it's gender
    <input type="radio" name="female" value="gender">زن//women
    <input type="radio" name="male" value="gender">مرد//man<br><br>
    <input type="button" value="محاسبه" onClick="cal()"><br>//calculator button
    <p id="calcu"></p>
  </fieldset>
</form>

3 个答案:

答案 0 :(得分:1)

您需要1:每次仅选择一个元素,然后2:将值转换为数字:

function cal() {
  var h = parseInt(document.getElementsByName("height")[0].value);
  var w = parseInt(document.getElementsByName("weight")[0].value);
  var r = (w / (h * h)) * 10000; //it's calculate MBI
  document.getElementById("calcu").innerHTML = r; //the answer shows under my form
  alert(r);
}
<form>
  <fieldset>
    <legend>محاسبه BMI</legend>//it's calculate BMI قد (cm)<br>//it's height
    <input type="number" name="height" placeholder="175" value="175"><br> وزن (kg)<br>//it's weight
    <input type="number" name="weight" placeholder="75" value="73"><br> جنسیت
    <br>//it's gender
    <input type="radio" name="female" value="gender">زن//women
    <input type="radio" name="male" value="gender">مرد//man<br><br>
    <input type="button" value="محاسبه" onClick="cal()"><br>//calculator button
    <p id="calcu"></p>
  </fieldset>
</form>

答案 1 :(得分:0)

document.getElementsByName返回元素的集合,而.value尚未定义。您应该使用

访问该元素的第一个元素
document.getElementsByName("...")[0].value

并使用+

将其转换为数字

function cal(){
	var h= +document.getElementsByName("height")[0].value;//it's get my height
	var w= +document.getElementsByName("weight")[0].value;//it's get my weight
	var r= (w/(h*h))*10000;//it's calculate MBI
	document.getElementById("calcu").innerHTML=r;//the answer shows under my form
	alert(r);
}
<form>
 			 <fieldset>
  			  <legend>محاسبه BMI</legend>//it's calculate BMI
  				  قد (cm)<br>//it's height
   				 <input type="number" name="height" placeholder="175" value="175"><br>
  				  وزن (kg)<br>//it's weight
   				 <input type="number" name="weight" placeholder="75" value="73"><br>
				 جنسیت<br>//it's gender
				 <input type="radio" name="female" value="gender">زن//women
				 <input type="radio" name="male" value="gender">مرد//man<br><br>
  				  <input type="button" value="محاسبه" onClick="cal()"><br>//calculator button
				 <p id="calcu"></p>
  			</fieldset>
			</form>

答案 2 :(得分:-1)

function cal(){
	var h= document.getElementsByName("height")[0].value;
	var w= document.getElementsByName("weight")[0].value;
	var r= (w/(h*h))*10000;
	document.getElementById("calcu").innerHTML=r;
	alert(r);
}
<div class="sectionmenu">
			<form>
 			 <fieldset>
  			  <legend>محاسبه BMI</legend>
  				  قد (cm)<br>
   				 <input type="number" name="height" placeholder="175" value="175"><br>
  				  وزن (kg)<br>
   				 <input type="number" name="weight" placeholder="75" value="73"><br>
				 جنسیت<br>
				 <input type="radio" name="female" value="gender">زن
				 <input type="radio" name="male" value="gender">مرد<br><br>
  				  <input type="button" value="محاسبه" onClick="cal()"><br>
				 <p id="calcu"></p>
  			</fieldset>
			</form> 
	
</div>