JavaScript和HTML按钮onclick()不起作用

时间:2018-07-21 16:22:37

标签: javascript html button onclick

所以我花了一个小时来弄清楚为什么当我单击按钮时什么也没发生...通常,用户应该输入一个数字,如果它是一个平方数,它将添加所有数字。我的代码什么都不做!请帮忙!

function analyzeNumber() {
  var input = document.getElementById("user_input").value;

  alert("User value" + input);

  document.getElementById("answer_display").innerHTML = input;

  if ((validateNumber(input)) == true) {
    if (Math.sqrt(input) % 1 === 0) {
      var total = sumDigits(input);
      return document.getElementById("answer_display").innerHTML = total;
    } else {
      return document.getElementById("answer_display").innerHTML = " ";
      alert("It is not a square number!");
    }
  }
}

function validateNumber(b) {
  if ((typeof.b == "number") && (b > -1)) {
    return true;
  } else {
    return false;
    alert("Please enter numeric value!");
  }
}

function sumDigits(a) {
  var str = input.toString();
  var sum = 0;
  for (i = 0; i < num.length; i++) {
    sum += parseInt(str.charAt(i));
  }
  return sum;
}
<head>
  <title> SOEN 287 Assignment 2 </title>
  <script type="text/javascript" src="Q1.js">
  </script>
</head>

<body action="" method="post">
  <fieldset>

    <h1> Question 1</h1>
    <form action="" id="sqrForm">
      <p>
        <label>Please enter a number: <input type = "text" id = "user_input" /></label>
        <input id="btton" type="submit" value="Try it!" onclick="return analyzeNumber()" />
      </p>
      <p>
        <label id="answer"> Answer: </label>
        <label id="answer_display"> </label>
      </p>
    </form>
  </fieldset>





</body>

2 个答案:

答案 0 :(得分:1)

  

typeof运算符后跟其操作数,其中   操作数是表示要返回其类型的对象或基元的表达式。

更改

if((typeof.b == "number") && (b > -1)){

收件人

if((typeof(b) == "number") && (b > -1)){

答案 1 :(得分:0)

关于未来的一些提示: 请勿使用alert进行调试,而应将console.log()console.info()与开发工具一起在浏览器中使用(按F12时将打开)。


您需要将typeof.b更改为typeof btypeof(b)

function analyzeNumber() {
  var input = document.getElementById("user_input").value;

  alert("User value" + input);

  document.getElementById("answer_display").innerHTML = input;

  if ((validateNumber(input)) == true) {
    if (Math.sqrt(input) % 1 === 0) {
      var total = sumDigits(input);
      return document.getElementById("answer_display").innerHTML = total;
    } else {
      return document.getElementById("answer_display").innerHTML = " ";
      alert("It is not a square number!");
    }
  }
}

function validateNumber(b) {
  if ((typeof b == "number") && (b > -1)) {
    return true;
  } else {
    return false;
    alert("Please enter numeric value!");
  }
}

function sumDigits(a) {
  var str = input.toString();
  var sum = 0;
  for (i = 0; i < num.length; i++) {
    sum += parseInt(str.charAt(i));
  }
  return sum;
}
<head>
  <title> SOEN 287 Assignment 2 </title>
  <script type="text/javascript" src="Q1.js">
  </script>
</head>

<body action="" method="post">
  <fieldset>

    <h1> Question 1</h1>
    <form action="" id="sqrForm">
      <p>
        <label>Please enter a number: <input type = "text" id = "user_input" /></label>
        <input id="btton" type="submit" value="Try it!" onclick="return analyzeNumber()" />
      </p>
      <p>
        <label id="answer"> Answer: </label>
        <label id="answer_display"> </label>
      </p>
    </form>
  </fieldset>





</body>


您应该注意的事情:

当您单击按钮触发脚本时,form提交,输出“消失”。为防止这种情况,请将type中的inputsubmit更改为button

function analyzeNumber() {
  var input = document.getElementById("user_input").value;

  alert("User value" + input);

  document.getElementById("answer_display").innerHTML = input;

  if ((validateNumber(input)) == true) {
    if (Math.sqrt(input) % 1 === 0) {
      var total = sumDigits(input);
      return document.getElementById("answer_display").innerHTML = total;
    } else {
      return document.getElementById("answer_display").innerHTML = " ";
      alert("It is not a square number!");
    }
  }
}

function validateNumber(b) {
  if ((typeof b == "number") && (b > -1)) {
    return true;
  } else {
    return false;
    alert("Please enter numeric value!");
  }
}

function sumDigits(a) {
  var str = input.toString();
  var sum = 0;
  for (i = 0; i < num.length; i++) {
    sum += parseInt(str.charAt(i));
  }
  return sum;
}
<head>
  <title> SOEN 287 Assignment 2 </title>
  <script type="text/javascript" src="Q1.js">
  </script>
</head>

<body action="" method="post">
  <fieldset>

    <h1> Question 1</h1>
    <form action="" id="sqrForm">
      <p>
        <label>Please enter a number: <input type = "text" id = "user_input" /></label>
        <input id="btton" type="button" value="Try it!" onclick="return analyzeNumber()" />
      </p>
      <p>
        <label id="answer"> Answer: </label>
        <label id="answer_display"> </label>
      </p>
    </form>
  </fieldset>
</body>