简体   繁体   中英

Undefined global variable in jQuery

I am a newbie to jQuery. I am trying to create a global variable so that I can pass it between pages. However, when I "alert" the variable in a document it is undefined.

Here is the script where I create the global variable (modalHandler.js):

var remainingCal;  

$(document).ready(function() {

    $(document).on("click", "#modal-btn", function() {

    $(".nav > li").first().next().remove();
    //get form inputs and calc remaining calories
    var gender = $("#gender").val();
    var age = getAge();
    var height = getHeight();
    var weight = getWeight();
    var goal = $("#loss-goal").val();
    var activity = $("#activity").val();
    remainingCal = calcDailyReq(gender, age, height, weight, activity, goal);


        jQuery('<li/>', {
    id: 'dashboard',
    }).appendTo(".nav").append("<a href='dashboard.html'>Dashboard</a>");

    var url = "dashboard.html"
    window.location.replace(url);

    });

});

function getAge() {
    ageCode = $("#age").val();
    var minAge = 18;
    var age = parseInt(ageCode) + minAge;
    return age;
}

function getHeight() {
    var heightCode = $("#height").val();
    var minHeight = 54;
    var height = parseInt(heightCode) + minHeight;
    return height;
}

function getWeight() {
    var weightCode = $("#weight").val();
    var minWeight = 100;
    var weight = parseInt(weightCode) + minWeight;
    return weight;
}

//Calculates daily calories using BMR, Harris Benedict Equation, and weight loss goal input
function calcDailyReq(gender, age, height, weight, activity, goal) {
    var bmr;
    var dailyReq;
    var poundOfFat = 3500;

    if (gender == "male") {
    bmr = 66 + (6.23 * weight) + (12.7 * height) - (6.8 * age);
    } else {
    bmr = 665 + (4.35 * weight) + (4.7 * height) - (4.7 * age);
    }

    if (activity == "0") {
    dailyReq = bmr * 1.2
    } else if (activity == "1") {
    dailyReq = bmr * 1.375;
    } else if (activity == "2") {
    dailyReq = bmr * 1.55;
    } else if (activity == "3") {
    dailyReq = bmr * 1.725;
    } else {
    dailyReq = bmr * 1.9;
    }

    var weeklyTarget = dailyReq - (poundOfFat * parseFloat(goal))/7;
    return Math.round(weeklyTarget);
}

Below is the html on the page in which I am trying to access the global remainingCal variable:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Online Calorie Tracker and Analyzer</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/dashboard.css">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/modalHandler.js"></script>
    <script src="js/dashboard.js"></script>
  </head>
  <body>
    <div class="container">
      <h1>Calorie Tracker and Analyzer</h1>
          <div class="navbar">
              <div class="navbar-inner">
                <div class="container">
                  <ul class="nav">
                    <li><a href="index.html">Home</a></li>
                       <li class="active"><a href="dashboard.html">Dashboard</a></li>
                    </ul>
                </div>
              </div>
            </div>
      <div class="hero-unit">
         <h3 id="track-head">Track What You Eat</h3>
          </div>
      </body>
<html>

The dashboard.js simply consists of:

alert(remainingCal);

which yields undefined in the page. Any help is appreciated!

Your remainingCal is set to a value inside a click handler assigned inside the $(document).ready( function.

When the DOM is ready the $(document).ready( function is ran. But, you still need to click on #modal-btn to run the function that assigns a value to remainingCal .

Welcome to asynchronous JavaScript! :-D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM