计算帮助需要与javascript目标心率计算器

calculation help needed with javascript target heart rate calculator

本文关键字:目标 标心率 计算器 javascript 帮助 计算      更新时间:2023-09-26

所以我有点新的javascript和我有一个问题与目标心率计算器我试图创建。当我点击计算按钮时,什么也没有发生。谁能告诉我我哪里做错了?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"lang="en">
<head>
<link href='http://fonts.googleapis.com/css?family=Nixie+One' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/workitt.css"> 
<script>
    function initTH(){
        document.getElementById("calcButton").onclick = thr;
    }
    function thr(){
        var age = document.getElementById("age").value;
        if(age = ""){
            document.getElementById("error").innerHTML = "This field is required.";
        }
        else{
            var THRmax = (220-age)*.85;
            var THRmin = (220-age)*.7;
            document.getElementById("THRzone").innerHTML = THRmin + "-" + THRmax;
        }
    }
</script>
<title>Workitt</title>
</head>
 <body>
    <CENTER><div class="header">
    <h1><img src="images/workitt-header.jpg" alt=header ></h1>
       <div class="navbar">
        <a href="workitt.html">Home</a> |
        <a href="profile.html">Profile</a> |
        <a href="createworkout.html">Create&nbsp;A&nbsp;Workout</a> |
        <a href="accessories.html">Fitness&nbsp;Accessories</a>
  </div>
  <p>&nbsp;</p>
  </div></CENTER>   
  <CENTER><div class="body">
  <form>
        <table class="table1" border="7" bordercolor=WHITE width="250" align="center" bgcolor="#ffffff">
          <thead>
            <tr>
              <th colspan="2"><font size=5>
                Target Heart Rate</font>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
          </thead>
          <tbody>
            <tr>
              <td align="center">
                <label for="age">Age:</label>
              </td>
              <td align="center">
                <input type="text" id="age" name="age" size="6"> years
              </td>
            </tr>
            </tbody>
            <thead>
            <tr>
              <th colspan="2" align="center">
                <input id="calcButton" type="button" value="calculate" />
                <span id="error"></span>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>
                <th colspan="2">
                    Your Target Heart Rate Zone is:&nbsp;<span id="THRzone"></span>
                </th>
            </tr>
        </thead>
    </table>
 </form> 
 <p>*Your target heart rate zone is the zone in which your heart rate should be in when exercising to have the most effective workout for improving your fitness and burning calories.</p> 
</div></CENTER>
</body>
</html>

您的脚本的问题,正如在评论中指出的那样,是您从未调用initTH(),因此按钮单击处理程序从未附加到按钮。

注释没有注意到的是,您的脚本将放在HTML的之前。特别是,它出现在<input id="calcButton" type="button" value="calculate" />之前,这意味着如果您在调用document.getElementById("calcButton")时不小心,它将返回null

听起来你想做的是在body的end添加一个额外的<script>元素(就在close </body>标记之前)并调用initTH():

    ....
    <script>
        initTH();
    </script>
</body>

正如其他人所提到的,initTH()函数永远不会被调用。

我在这里做了一些改变:http://jsfiddle.net/GaryHarrower/j4v7M/

我删除了这个功能,所以只要按下按钮,它就会运行。

脚本中还有age = "",这应该是带有2 =符号的age == ""

让我知道你进展如何!

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"lang="en">
    <head>
        <link href='http://fonts.googleapis.com/css?family=Nixie+One' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" href="css/workitt.css"> 
        <title>Workitt</title>
    </head>
    <body>
        <CENTER>
            <div class="header">
                <h1><img src="images/workitt-header.jpg" alt=header ></h1>
                <div class="navbar">
                    <a href="workitt.html">Home</a> |
                    <a href="profile.html">Profile</a> |
                    <a href="createworkout.html">Create&nbsp;A&nbsp;Workout</a> |
                    <a href="accessories.html">Fitness&nbsp;Accessories</a>
                </div>
                <p>&nbsp;</p>
            </div>
        </CENTER>   
        <CENTER>
            <div class="body">
                <form>
        <table class="table1" border="7" bordercolor=WHITE width="250" align="center" bgcolor="#ffffff">
          <thead>
            <tr>
              <th colspan="2"><font size=5>
                Target Heart Rate</font>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
          </thead>
          <tbody>
            <tr>
              <td align="center">
                <label for="age">Age:</label>
              </td>
              <td align="center">
                <input type="text" id="age" name="age" size="6"> years
              </td>
            </tr>
            </tbody>
            <thead>
            <tr>
              <th colspan="2" align="center">
                <input id="calcButton" type="button" value="calculate" />
                <span id="error"></span>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>
                <th colspan="2">
                    Your Target Heart Rate Zone is:&nbsp;<span id="THRzone"></span>
                </th>
            </tr>
        </thead>
    </table>
 </form> 
 <p>*Your target heart rate zone is the zone in which your heart rate should be in when exercising to have the most effective workout for improving your fitness and burning calories.</p> 
</div></CENTER>
<script>
            document.getElementById("calcButton").onclick = thr;
            function thr(){
                var age = document.getElementById("age").value;
                if(age == ""){
                    document.getElementById("error").innerHTML = "This field is required.";
                }
                else
                {
                    var THRmax = (220-age)*.85;
                    var THRmin = (220-age)*.7;
                    document.getElementById("THRzone").innerHTML = THRmin + "-" + THRmax;
                }
            }
        </script>
</body>
</html>