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

tec*_*.in 0 html javascript

嗨,大家好,我正在使用身高和体重来计算BMI,它工作正常,但我想将输入的身高(厘米)转换为英尺

的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/>
Run Code Online (Sandbox Code Playgroud)

Java脚本

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")
 } 
}
Run Code Online (Sandbox Code Playgroud)

Duš*_*chý 6

尝试这个

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;';
    }
Run Code Online (Sandbox Code Playgroud)

  • 这确实-&gt; http://stackoverflow.com/questions/13723585/javascript-jquery-display-inches-in-feet-and-inches (3认同)
  • @RazvanDumitru如果答案没有错误,请不要拒绝投票。 (2认同)