为什么我的函数在我的计算器html代码中没有被调用?

时间:2017-10-11 06:08:24

标签: javascript html

Memory = "0";
Current = "0";
Operation = 0;
MAXLENGTH = 30;
alert("yea");

function AddDigit(digit) {
  alert("yea");
  if (Current.length > MAXLENGTH) {
    Current = "Aargh! Too long";
  } else {
    if (eval(Current) == 0) {
      Current = dig;
    } else {
      Current = Current + dig;
    }
  }
}

document.Calculator.Display.value = Current;
}
<FORM name="Calculator">
  <table border="3">
    <tr>
      <td colspan="2">
        <input type="text" maxlength="40" size="30" name="Display">
      </td>
    </tr>
    <table>
      <tr>
        <td><input type="button" value="7"></td>
        <td><input type="button" value="8"></td>
        <td><input type="button" value="9"></td>
        <td><input type="button" value="+"></td>
      </tr>
      <tr>
        <td><button onclick="AddDigit(4)">4</button></td>
        <td><input type="button" value="5"></td>
        <td><input type="button" value="6"></td>
        <td><input type="button" value="-"></td>
      </tr>
      <tr>
        <td><input type="button" value="1"></td>
        <td><input type="button" value="2"></td>
        <td><input type="button" value="3"></td>
        <td><input type="button" value="*"></td>
      </tr>
      <tr>
        <td><input type="button" value="0"></td>
        <td><input type="button" value="."></td>
        <td><input type="button" value="="></td>
        <td><input type="button" value="/"></td>
      </tr>
      <tr>
        <td><input type="button" value="C"></td>
        <td><input type="button" value="MR"></td>
        <td><input type="button" value="M-"></td>
        <td><input type="button" value="M+"></td>
      </tr>
      <tr>
        <td><input type="button" value="MC"></td>
      </tr>
    </table>
    </td>
</FORM>

我想知道为什么当我按下计算器中的4按钮时显示屏上没有显示任何内容。

在网络浏览器栏中,我只看到display=,然后在实际显示中没有显示任何内容。我添加了alert("yea")以检查函数是否在运行,甚至没有点击运行。

所以我不知道这里发生了什么。

1 个答案:

答案 0 :(得分:0)

您需要在}

中的display.value之前移除javascript code

您更改了<input type="button" onclick="AddDigit(4)" value="4">

<button onclick="AddDigit(4)">4</button>内容

并将javascript中的dig variable更改为digit

Memory = "0";
Current = "0";
Operation = 0;
MAXLENGTH = 30;
alert("yea");

function AddDigit(digit) {
  alert("yea");
  if (Current.length > MAXLENGTH) {
    Current = "Aargh! Too long";
  } else {
    if (eval(Current) == 0) {
      Current = digit;

    } else {
      Current = Current + digit;

    }
  }
  document.Calculator.Display.value = Current;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<script src="./calculator.js"></script>
<FORM name="Calculator">
  <table border="3">
    <tr>
      <td colspan="2">
        <input type="text" maxlength="40" size="30" name="Display">
      </td>
    </tr>
    <table>
      <tr>
        <td><input type="button" value="7"></td>
        <td><input type="button" value="8"></td>
        <td><input type="button" value="9"></td>
        <td><input type="button" value="+"></td>
      </tr>
      <tr>
        <td><input type="button" onclick="AddDigit(4)" value="4"></td>
        <td><input type="button" value="5"></td>
        <td><input type="button" value="6"></td>
        <td><input type="button" value="-"></td>
      </tr>
      <tr>
        <td><input type="button" value="1"></td>
        <td><input type="button" value="2"></td>
        <td><input type="button" value="3"></td>
        <td><input type="button" value="*"></td>
      </tr>
      <tr>
        <td><input type="button" value="0"></td>
        <td><input type="button" value="."></td>
        <td><input type="button" value="="></td>
        <td><input type="button" value="/"></td>
      </tr>
      <tr>
        <td><input type="button" value="C"></td>
        <td><input type="button" value="MR"></td>
        <td><input type="button" value="M-"></td>
        <td><input type="button" value="M+"></td>
      </tr>
      <tr>
        <td><input type="button" value="MC"></td>
      </tr>
    </table>
    </td>
</FORM>

</html>