使用Javascript将高度(厘米)转换为英尺

时间:2016-10-12 10:17:24

标签: javascript html

大家好我正在使用身高和体重计算BMI它工作正常,但我想在Feet'inches中转换输入的身高(厘米)

HTML

//height in Centimeter
<input type="number" onblur="myFunction()" min="0" name="height" id=height_' + len + ' class="txtInput2 personalize-form box-width" required/>

 //Weight
<input type="number" onblur="myFunction()" min="0" name="weight" id=weight_' + len + ' class="txtInput4 personalize-form box-width" required/>

的Javascript

function myFunction(){
  var height = document.getElementById("height_1").value;
  var weight = document.getElementById("weight_1").value;

  if(weight > 0 && height > 0){ 
   var finalBmi = weight/(height/100*height/100)
     document.bmiForm.bmi.value = finalBmi
   if(finalBmi < 18.5){
    var health= document.bmiForm.meaning.value = "UW"
   }

   if(finalBmi > 18.5 && finalBmi < 25){
       var health1= document.bmiForm.meaning.value = "IW"
       alert(health1);
     }
   if(finalBmi > 25){
     var health2= document.bmiForm.meaning.value = "OW"
      alert(health2);
    }
  }
else{
  //alert("Please Fill in everything correctly")
 } 
}

1 个答案:

答案 0 :(得分:5)

试试这个

function toFeet(n) {
      var realFeet = ((n*0.393700) / 12);
      var feet = Math.floor(realFeet);
      var inches = Math.round((realFeet - feet) * 12);
      return feet + "&prime;" + inches + '&Prime;';
    }