javascript - 简单的文本字段计算不起作用

标签 javascript html css text

我已经对此进行了相当多的了解,这似乎是一个简单的计算,但由于某种原因,我的代码没有产生我需要的结果。

我正在尝试创建一个执行以下操作的计算器。用户输入他们的体重和体脂百分比。他们还输入建议的每日水分比例和每日蛋白质比例。

接下来,计算器将输入输入的体重和体脂百分比,并将其相乘,得到脂肪重量的磅数,然后将体重减去脂肪重量的磅数,得出瘦体重质量。

接下来,它会将去脂体重乘以建议的每日饮水比例,以计算每日饮水量。

最后,它将去脂体重乘以建议的每日蛋白质比例,以计算每日蛋白质摄入量。

这是我迄今为止所拥有的。

非常感谢任何帮助。谢谢

<!DOCTYPE html>

<html>


<head>
<title>Dr. Kosimides Lean Body Academy Calculator</title>


<style type = "text/css">
    body
    {
        background:#FFC;
    }

    div
    {
        border:2px solid #F69;
        padding:10px 40px;
        background:#FCC;
        width:600px;
        border-radius:20px;
        box-shadow: 10px 10px 2px #CCC;
        text-align: center;
        margin-right:auto;
        margin-left:auto;
    }

    h1
    {
        color:#F06
        text-align:center;
    }

    </style>
    </head>

    <body>

        <div>
        <form>
            <h1>LBA Calculator</h1>
            <br />
            <label> Weight
                    <input type="text" name="weight" id="weight" /> (lbs)
            </label>
            <label> Body Fat
                    <input type="text" name="bodyfat" id="bodyfat" /> (%)
            </label>
            <br />
            <br />
             <label> Protein
                    <input type="text" name="protein" id="protein" />
            </label>
            <br />
             <label> Water
                    <input type="text" name="water" id="water" />
            </label>
            <br />
            <br />
             <label> Pounds of Body Fat
                    <input type="text" name="lbf" id="lbf" />
            </label>
            <br />
            <br />
            <label> Lean Body Mass
                    <input type="text" name="lbm" id="lbm" />
            </label>
            <br />
            <br />
            <label> Daily Water Intake
                    <input type="text" name="dwi" id="dwi" />
            </label>
            <br />
            <br />
            <label> Daily Protein Intake
                    <input type="text" name="dpi" id="dpi" />
            </label>
            <input type="button" name="Calculate" value="Calculate" onClick="LBAcalc()" />
            <input type="reset" name="Reset" value="reset" />
        </form>
        </div>

        <script>

            function LBAcalc()
            {


                     // Get The Input

                    var w_txt = document.getElementById('weight');
                    var bf_txt = document.getElementById('bodyfat');
                    var p_txt = document.getElementById('protein');
                    var h20_txt = document.getElementById('water');


                     // Convert To Numbers If Needed

                    var w = parseInt(w_txt.value);
                    var bf = parseInt(bf_txt.value);
                    var p = parseInt(p_txt.value);
                    var h20 = parseInt(h20_txt.value);


                    // Convert body fat to percentage

                    bf = bf / 100;


                    // Validate Inputs

                    var errMsg = "";

                    if (w <= 0)
                      errMsg = errMsg + "*Weight cannot be negative \n";

                    if (w == "")
                      errMsg = errMsg + "*Weight must be entered \n";

                    if ( bf <= 0)
                      errMsg = errMsg + "*Body Fat cannot be negative \n";     


                    // If Error Inform User

                    if (errMsg != "")

                        alert(errMsg);

                    else


                    // If No Error Process and Display Results

                    var result = document.getElementById ('lbf');
                    var myResult = w * bf;
                    result.value = myResult.toFixed(1);

                    var result1 = document.getElementById ('lbm');
                    var myResult1 = w - myResult;
                    result1.value = myResult1.toFixed(1);

                    var result2 = document.getElementById ('dwi');
                    var myResult2 = w * myResult1;
                    result2.value = myResult2.toFixed(1);

                    var result3 = document.getElementById ('dpi');
                    var myResult3 = myResult * p;
                    result3.value = myResult3.toFixed(1);


            }



        </script>

</body>
</html>

最佳答案

到目前为止你所拥有的一切都还好。唯一的问题是,您忘记将多个元素包含在 else 中:

else {
  // If No Error Process and Display Results

  var result = document.getElementById ('lbf');
  var myResult = w * bf;
  result.value = myResult.toFixed(1);

  var result1 = document.getElementById ('lbm');
  var myResult1 = w - myResult;
  result1.value = myResult1.toFixed(1);

  var result2 = document.getElementById ('dwi');
  var myResult2 = w * myResult1;
  result2.value = myResult2.toFixed(1);

  var result3 = document.getElementById ('dpi');
  var myResult3 = myResult * p;
  result3.value = myResult3.toFixed(1);
}

如果您没有多个声明,那完全没问题。就像前面的 if 语句中的情况一样。但是当有多个时,您需要使用{ }将它们括起来。如果不是,则对于 else 条件,它将仅执行第一个语句,并执行第一个之后的所有语句,无论条件如何。

关于javascript - 简单的文本字段计算不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37051325/

相关文章:

javascript - 我不明白 Eloquent js "a list"练习(我有一些奇怪的结果)

javascript - 从具有特定 http 状态的 express 中间件退出

javascript - 使用 Javascript 根据 <li class = 'bu' > 的数据值构建数组

html - 无法拆分行和列的 div

Jquery fadeIn() 不透明度 :0 or visibility:hidden

javascript - 位置 Java 脚本在部署后停止工作

javascript - base64 中的图像未在 Safari 中显示

css - 当鼠标在 div 内时,如何使 div 内的所有链接改变颜色?

javascript - 响应 Div 滚动

html - IMG 和 TD 的 CSS 宽度/高度