我有一个完美运行的BMI计算器,但我的客户现在想让用户能够从其当前CMs + KGs的测量值切换到提供石头,英尺和英寸。我不完全确定如何转换它。
我的代码如下:
$("#height").bind(
"slider:changed", function (event, data) {
$("#height_value").html(data.value.toFixed(0));
calculateBMI();
}
);
$("#weight").bind(
"slider:changed", function (event, data) {
$("#weight_value").html(data.value.toFixed(0));
calculateBMI();
}
);
function calculateBMI(){
var weight = $("#weight_value").html();
var height = parseInt($("#height_value").html())/100;
var bmi = weight / (height * height);
var type = "Normal";
var imageResult = $('#image-result');
var classes = 'underweight overweight obese very-obese normal-weight';
if (bmi < 15){
type= "Score below 20: Underweight";
imageResult.removeClass(classes);
imageResult.addClass('underweight');
}else if(bmi <=24.9){
type= "Score: 20-25: Normal Weight";
imageResult.removeClass(classes);
imageResult.addClass('normal-weight');
}else if(bmi <=29.9){
type= "Score 26-30:Overweight";
imageResult.removeClass(classes);
imageResult.addClass('overweight');
}else if(bmi <=34.9){
type= "Score 31-40:Obese";
imageResult.removeClass(classes);
imageResult.addClass('obese');
}else{
type= "Score: 40+:Very Obese";
imageResult.removeClass(classes);
imageResult.addClass('very-obese');
}
$("#result").html("<small>"+type+"</small>");
$('#bmi-index').html(bmi.toFixed(1));
}
calculateBMI();
非常感谢您的帮助!
答案 0 :(得分:0)
没关系。我设法通过插入这个来转换它:
$("#height").bind(
"slider:changed", function (event, data) {
$("#height_value").html(data.value.toFixed(0));
var inches = (data.value*0.393700787).toFixed(0);
var feet = Math.floor(inches / 12);
inches %= 12;
$("#height_value_in").html(feet + "ft " + inches + 'in');
calculateBMI();
}
);
$("#weight").bind(
"slider:changed", function (event, data) {
$("#weight_value").html(data.value.toFixed(0));
$("#weight_value_pounds").html((data.value*2.20462).toFixed(0) + 'lb' );
calculateBMI();
}
);