Javascript警报无法正常工作

时间:2014-12-11 22:03:53

标签: javascript html function

<html>

<body>

  <p>Enter names in the fields, then click "Submit" to submit the form:</p>


  <form name="form">

    <input type="text" id="birthDate">

    <br> Current Date

    <br>

    <input type="text" id="currentDate">

    <br>

    <br>

    <br>

    <a id="Submit_Button" onclick="test();" href="javascript:void(0);" title="Submit">Submit</a>

  </form>
  <script>
    function test() {


      var birthDate = document.getElementById("birthDate");

      var currentDate = document.getElementById("currentDate");




      var ageDate = birthDate.value - currentDate.value;


      function ageD(ageDate) {
        if (ageDate < 1) {
          return function calculate(y) {
            Math.abs(ageDate) = y
          }
        } else {
          return function calculate(y) {
            ageDate = y
          }
        }
      }
      var ageDale = ageD();
      var x = ageDale(ageDate);



      alert(x)
    }
  </script>

</body>

</html>

我似乎无法解决这个问题。我一直对x未定义,它可能很简单,但我很感激帮助。

这可能与我的功能有关,因为我在设置它时并不知道我在做什么。

5 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解你的逻辑,但ageD返回的函数反过来会返回一些值,因为你显然希望它们产生一些东西:

var x = ageDale(ageDate);

目前这些calculate函数未返回任何内容,因此xundefined。看起来你想要这样的东西:

function ageD(ageDate) {
    if (ageDate < 1) {
        return function calculate(y) {
            return Math.abs(ageDate);
        }
    } else {
        return function calculate(y) {
            return y;
        }
    }
}

答案 1 :(得分:0)

未定义函数AgeDale。您需要调用函数,而不是名为ageDale

的变量

答案 2 :(得分:0)

函数ageD()需要一个参数而你没有传递任何东西,所以函数总是返回else语句。

你正在向x变量转移该函数的返回值,但它不会返回任何内容

答案 3 :(得分:0)

如果您只是想要了解用户的年龄,您可以让他们进入一年,如1990年和2014年,并且这个:

function test() {
  var birthDate = document.getElementById("birthDate");
  var currentDate = document.getElementById("currentDate");
  var ageDate =  currentDate.value - birthDate.value;
  function ageD(ageDate) {
    if (ageDate < 1) {
      return function calculate(y) {
        Math.abs(ageDate) = y
      }
    } else {
      return ageDate
    }
  }
  var ageDale = ageD();
  var x = ageDate;
  alert(x)
}

否则,如果你想让他们输入像dd / mm / yyyy这样的日期,你就会有不同的东西。

答案 4 :(得分:0)

function test() {
  // convert the input into intiger
  var birthDate = parseInt(document.getElementById("birthDate").value, 10);
  var currentDate = parseInt(document.getElementById("currentDate").value, 10);
  var ageDate = birthDate - currentDate;

  function ageD(ageDate) {
    return Math.abs(ageDate);
  }
  var x = ageD(ageDate);
  alert(x);
}