JavaScript警报中的NaN

时间:2016-12-21 11:50:24

标签: javascript html

我希望在textbox中获得生育年份,并在alert中显示年龄,但会显示NaN



function getAge(birth){
    //get current date
    var now =  new Date();
    var currentYear = now.getFullYear();
    //get bithday 
    var birthYear = birth;
    //calc
    var age = currentYear - birthYear;
    // var age_month = currentMonth - birthmonth;
    return age;
}

function getIn(){
    var getTextBox = document.getElementById("txtInput");
    var getVal = getTextBox.value;
    var birth = new Number(getVal);
    alert (getAge());
}

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="javascript.js"></script>
    </head>
    <body>
        <form>
            <input type="text" id="txtInput"/>
            <input type="button" value="Calc" onclick="getIn()"/>
        </form>
    </body>
</html>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:3)

alert (getAge(birth));似乎解决了这个问题? getAge期望将birth的值传递给它。

function getAge(birth){
    //get current date
    var now =  new Date();
    var currentYear = now.getFullYear();
   //get bithday 
    var birthYear = birth;
    //calc
    var age = currentYear - birthYear;
  //  var age_month = currentMonth - birthmonth;
    return age;
}

function getIn(){
    var getTextBox = document.getElementById("txtInput");
    var getVal = getTextBox.value;
    var birth = new Number(getVal);//tabdil b addad
    alert (getAge(birth));
}
<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="javascript.js"></script>
    </head>
    <body>
        <form>
            <input type="text" id="txtInput"/>
            <input type="button" value="Calc" onclick="getIn()"/>
        </form>
    </body>
</html>

答案 1 :(得分:1)

您没有向getAge()函数传递任何内容,这将导致NaN。

答案 2 :(得分:0)

不需要新的号码只使用Number()。你也错过了将参数传递给函数。

我附上了更正的代码。

<!DOCTYPE html>
<html>

<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
        function getAge(birth) {
            //get current date
            var now = new Date();
            var currentYear = now.getFullYear();
            //get bithday 
            var birthYear = birth;
            //calc
            var age = currentYear - birthYear;
            //  var age_month = currentMonth - birthmonth;
            return age;
        }

        function getIn() {
            var getTextBox = document.getElementById("txtInput");
            var getVal = getTextBox.value;
            var birth = Number(getVal); //tabdil b addad
            alert(getAge(birth));
        }
    </script>
</head>

<body>
    <form>
        <input type="text" id="txtInput" />
        <input type="button" value="Calc" onclick="getIn()" />
    </form>
</body>

</html>

答案 3 :(得分:0)

function getAge(birth) {
  //get current date
  var now = new Date();
  var currentYear = now.getFullYear();
  //get bithday 
  var birthYear = birth;
  //calc
  var age = currentYear - birthYear;
  // var age_month = currentMonth - birthmonth;
  return age;
}

function getIn() {
  var getTextBox = document.getElementById("txtInput");
  var getVal = getTextBox.value;
  var birth = new Number(getVal); //tabdil b addad
  alert(getAge(birth));
}
<form>
  <input type="text" id="txtInput" />
  <input type="button" value="Calc" onclick="getIn()" />
</form>

试试这个。

将参数传递给getAge(birth)函数 因为你需要参数来计算最终值

`var birthYear = birth;
      //calc
      var age = currentYear - birthYear;`

答案 4 :(得分:0)

问题是可变范围。您在birth中声明getIn,然后尝试在getAge中读取它,但您没有在它们之间传递变量。

function getAge(birth) {
  //get current date
  var now = new Date();
  var currentYear = now.getFullYear();
  //get bithday 
  var birthYear = birth;
  //calc
  var age = currentYear - birthYear;
  // var age_month = currentMonth - birthmonth;
  return age;
}

function getIn() {
  var getTextBox = document.getElementById("txtInput");
  var getVal = getTextBox.value;
  var birth = new Number(getVal); //tabdil b addad
  alert(getAge(birth));
}

关键是最后一行。我将出生的副本传递给getAge函数。