我正在用HTML创建一个网页,其中包含javascript和带有提交按钮的小表单,该按钮调用了整个javascript代码的功能。

这应该作为BMR +维护热量计算器。使用以下公式显示:


http://www.bmi-calculator.net/bmr-calculator/bmr-formula.php
http://www.bmi-calculator.net/bmr-calculator/harris-benedict-equation/


男性公制BMR = 66 +(13.7 x千克重量)+(5 x高度厘米)-(6.8 x年龄单位)

女性公制BMR = 655 +(9.6 x公斤重量)+(1.8 x高度厘米)-(4.7 x年龄单位)

然后要发现一些日常维护热量需求(保持相同的体重),需要根据活动水平进行乘数计算。正是在创建这些IF语句时,我才开始遇到这些问题。

问题:当我单击提交时,什么都没有发生。它曾经将用户发送到显示文档的新页面。编写文本,但是现在,它什么也不做。

我认为这是一个相当简单的问题,但我对javascript还是陌生的,因此将不胜感激,非常感谢!

<html>
<head>
<title>BMR Calculator</title>
</head>
<body>

<h1> BMR Calculator and Daily Maintenance Calorie Calculator </h1>

<script type="text/javascript" language="javascript">

function Calculate() {

    var gender = document.getElementById("gender").value;
    var weight = document.getElementById("weight").value;
    var height = document.getElementById("height").value;
    var age = document.getElementById("age").value;
    var result = document.getElementById("result").value;
    var MaintenanceCalories = document.getElementById("MaintenanceCalories").value;
    var activitylevel = document.getElementById("activitylevel").value;


    if(gender=="male")
    //English-BMR = 66 + ( 6.23 x weight in pounds ) + ( 12.7 x height in inches ) - ( 6.8 x age in year )
    //Metric-BMR = 66 + ( 13.7 x weight in kilos ) + ( 5 x height in cm ) - ( 6.8 x age in years )
        {
            val1 = 13.7 * weight;
            val2 = 5 * height;
            val3 = 6.8 * age;
            result = 66 + val1 + val2 - val3;
            val4 = activitylevel;
        }

    else if (gender=="female")
    //English-Women: BMR = 655 + ( 4.35 x weight in pounds ) + ( 4.7 x height in inches ) - ( 4.7 x age in years)
    //Women: BMR = 655 + ( 9.6 x weight in kilos ) + ( 1.8 x height in cm ) - ( 4.7 x age in years )
        {
            val1 = 9.6 * weight;
            val2 = 1.8 * height;
            val3 = 4.7 * age;
            result = 655 + val1 + val2 - val3;
            val4 = activitylevel;
        }

    if(val4=="l")
    {
        MaintenanceCalories = result * 1.2;
    }

    if(val4=="lm")
    {
        MaintenanceCalories = result * 1.375;
    }

    if(val4=="m")
    {
        MaintenanceCalories = result * 1.55;
    }

    if(val4=="mh")
    {
        MaintenanceCalories = result * 1.725;
    }

    else if(val4=="h")
    {
        MaintenanceCalories = result * 1.9;
    }

    document.write ('Your BMR is: ' + result.toFixed(2) + '. Your Maintenance Calories are: ' + MaintenanceCalories.toFixed(2));


}
</script>


<form action="#">
    Gender  : <select id="gender">
        <option value="male">Male</option>
        <option value="female">Female</option>
        </select><br/>
    Weight (kg.)   : <input type="text" id="weight" /><br />
    Height (cm): <input type="text" id="height" /><br />
    Age     : <input type="text" id="age" /><br />
    Current Activity Level: <select id="activitylevel">
        <option value="l">Sedentary (Little or No Exercise)</option>
        <option value="lm">Lightly Active (Light Exercise/Sports 1-3 Days Per Week)</option>
        <option value="m">Moderately Active (Moderate Exercise/Sports 3-5 Days Per Week)</option>
        <option value="mh">Very Active (Hard Exercise/Sports 6-7 days Per Week)</option>
        <option value="h">Extra Active (Very Intense Exercise/Sports and Physical Job Daily)</option>
    </select>
    </br>
    </br>

    </fieldset>
    <input type="button" value="Submit" onclick="Calculate()"/>
</form>

最佳答案

你的问题就在网上

document.write ('Your BMR is: ' + result.toFixed(2)'. Your Maintenance Calories are: ' + MaintenanceCalories.toFixed(2));

您缺少+的地方:

 document.write ('Your BMR is: ' + result.toFixed(2) + '. Your Maintenance Calories are: ' + MaintenanceCalories.toFixed(2));


另外,在给定的示例中,您缺少一些使脚本工作所需的html元素。 #result#MaintenanceCalories
我像这样添加了它们,效果很好:

<div id="result"></div>
<div id="MaintenanceCalories"></div>


另一方面,如果您使用空字符串初始化这些值(也无需再添加html标记)也可以:

var result = '';
var MaintenanceCalories = '';


此外,为了将表单保留在页面上,可以在表单的结束标记后添加一个div,可以在其中显示结果:

<div id="results"></div>


和JavaScript,位于Calculate()函数的末尾:

results.innerHTML ='Your BMR is: ' + result.toFixed(2) + '. Your Maintenance Calories are: ' + MaintenanceCalories.toFixed(2);

09-12 08:24