Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数公式不';它不应该工作_Javascript_Jquery - Fatal编程技术网

Javascript 函数公式不';它不应该工作

Javascript 函数公式不';它不应该工作,javascript,jquery,Javascript,Jquery,我有这个公式,我不明白为什么它给出了一个不正确的结果。代码如下: //gebder 0=male 1=female $(document).ready(function() { $('#met_inputs').hide(); $('#unit_std').prop('checked', true); var standardUnits = true; $('input[name="unit"]:radio').change(function() { if ($(this).val()

我有这个公式,我不明白为什么它给出了一个不正确的结果。代码如下:

//gebder 0=male 1=female

$(document).ready(function() {
$('#met_inputs').hide();
$('#unit_std').prop('checked', true);
var standardUnits = true;
$('input[name="unit"]:radio').change(function() {
    if ($(this).val() === "met") {
        standardUnits = false;
        $('#standard_inputs').hide();
        $('#met_inputs').show();
    } else {
        standardUnits = true;
        $('#standard_inputs').show();
        $('#met_inputs').hide();
    }
});
var BMI = 0;
var RMR = 0;
$("#cal_btn").click(function() {
    if (standardUnits) {
        BMI = BMIStd();
    } else {
        BMI = BMIMet();
    }


    BMI = BMI.toFixed(1);

    RMR = getRMR().toFixed(1);

    //make all inputs black
    $("input[type='text']").each(function() {
        $(this).css('border-color', 'black');
    });


    if (isNaN(BMI) || isNaN(RMR)) {
        $('#results').html('<h2>Please fill-up the required field.</h2>');
        $('#results').css('color', 'red');

    //make blank inputs red
        $("input[type='text']").each(function() {
            if ($(this).val() === '') {
                $(this).css('border-color', 'red');
            }
        });
        return;
    } else {
        $('#results').css('color', 'black');
    }

    $("#results").html("<h2>Your BMI is " + BMI+"</h2>");

    if (BMI > 25) {
        $('#results').append("You are <b>above</b> the Normal BMI Range. Lose at least <b>" +    getLoss().toFixed(1) + "</b> to reach Normal BMI Range. Read these <a href='http://www.calculator-bmi.com/category/how-to-lose-weight/'>posts that will help you to lose weight. </a>");

 //$('#suggestion').append("<br><br>Your Resting Metabolic Rate (<i>Calories your body use</i>) is = <b>"+RMR+"</b>. This will be your guide if you want to lose, gain or maintain your weight.");
    } else if (BMI < 25 && BMI > 18.5) {
        $('#results').append("You weight is great! Just maintain what you're doing. Read more on <a href='http://www.calculator-bmi.com/category/how-to-maintain-weight/'>How to Maintain Weight</a> for optimal health.");
    } else if (BMI <= 18.4) {
        $('#results').append("You are <b>below</b> the Normal BMI Range. Gain at least <b>" + getGain().toFixed(1) + "</b> to reach Normal BMI Range. Read these <a href='http://www.calculator-bmi.com/category/how-to-gain-weight/'>posts on How to Gain Weight. </a>");

    }
    $('#suggestion').html("Your <b>Basal Metabolic Rate</b> (Calories your body use) is <b>" + RMR + "</b>. This will be your guide if you want to lose, gain or maintain your weight.");
});


function BMIMet() {
    var weight = parseFloat($("#met_weight").val());
    var height = parseFloat($("#met_height").val());
    return weight * 10000 / Math.pow(height, 2);
}

function BMIStd() {
    var weight = parseFloat($("#std_weight").val());
    var height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
    return weight * 703 / Math.pow(height, 2);
}

function getRMR() {
    var weight = 0;
    var height = 0;
    var age = parseFloat($('#age').val());
    if (standardUnits) {
        weight = parseFloat($("#std_weight").val());
        height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
    } else {
        weight = LbtoKg(parseFloat($("#met_weight").val()));
        height = IntoCm(parseFloat($("#met_height").val()));
    }



    if (getGender() === '0') {
        return (66 + (13.7 * weight) + (5 * height)) - (6.8 * age);
    } else {
        return 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
    }
}

function getLoss() {
    if (standardUnits) {
        weight = parseFloat($("#std_weight").val());
        height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
        return weight - (25 / 703) * height * height;
    } else {
        weight = parseFloat($("#met_weight").val());
        height = parseFloat($("#met_height").val());
        return weight - (25 / 10000) * height * height;
    }
}

function getGain() {
    if (standardUnits) {
        weight = parseFloat($("#std_weight").val());
        height = parseFloat($("#std_height_ft").val()) * 12 + parseFloat($("#std_height_in").val());
        return ((18.5 / 703) * height * height) - weight;
    } else {
        weight = parseFloat($("#met_weight").val());
        height = parseFloat($("#met_height").val());
        return ((18.5 / 10000) * height * height) - weight;
    }
}

function LbtoKg(val) {
    return parseFloat(val) * 2.20462;
}

function IntoCm(val) {
    return parseFloat(val) * 0.393701;
}

function getGender() {
    return $('select[name="gender"]').val();
}



});

什么是使用语言?你说的“使用语言”是什么意思?你是说jquery还是什么?我很确定这是JavaScript,特别是jquery,我已经编辑了这篇文章。如果我错了,请有人重新编辑帖子,添加正确的标签。是的。这是一个javascript。谢谢你的编辑,没问题。为了将来的参考,我建议添加一两个标记来指示代码使用的语言;这将吸引熟悉该语言的人,这将为你提供更好的答案。这也会节省你的时间,因为没有人会问你使用的是哪种语言。
    if (getGender() === '0') {
        return (66 + (13.7 * weight) + (5 * height)) - (6.8 * age);
    } else {
        return 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age);
    }