简体   繁体   中英

Inner.HTML shown of result flicker

function BMI(){
var h = document.getElementById('height').value;
var w = document.getElementById('weight').value;

  var bmi= w/(h/100*h/100);
  var bmi2 = (bmi.toFixed(2));

document.getElementById("result").innerHTML = " your BMI is " + bmi2;

}

why the output of "result" is flickering

You should convert the String into Number because in Javascript when you try to get the value from an input it is in String by default. And use template literals instead of concatenating the Strings.

function BMI(event){
  event.preventDefault();
  var h = Number(document.getElementById('height').value);
  var w = Number(document.getElementById('weight').value);

  var bmi= w/(h/100*h/100);
  var bmi2 = (bmi.toFixed(2));

  document.getElementById("result").innerHTML = `Your BMI is ${bmi2}`;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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