在Javascript / HTML计算器中清除值

京东

为什么当我按“ c”或“ =”时,计算器不能清除?

<form name='calculator'>
  <table>
    <tr>
      <td colspan='4'>
        <input type='text' name='display' id='display' disabled>
      </td>
    </tr>
    <tr>
      <td><input type='button' name='one' value='1' onclick="calculator.display.value += '1'"></td>

      <td><input type='button' name='two' value='2' onclick="calculator.display.value += '2'"></td>

      <td><input type='button' name='three' value='3' onclick="calculator.display.value += '3'"></td>

      <td><input type='button' class='operator' name='plus' value='+' onclick="calculator.display.value += '+'"></td>
    </tr>
    <tr>
      <td><input type='button' name='four' value='4' onclick="calculator.display.value += '4'"></td>

      <td><input type='button' name='five' value='5' onclick="calculator.display.value += '5'"></td>

      <td><input type='button' name='six' value='6' onclick="calculator.display.value += '6'"></td>

      <td><input type='button' name='minus' value='-' onclick="calculator.display.value += '-'"></td>
    </tr>
    <tr>
      <td><input type='button' name='seven' value='7' onclick="calculator.display.value += '7'"></td>

      <td><input type='button' name='eight' value='8' onclick="claculator.display.value += '8'"></td>

      <td><input type='button' name='nine' value='9' onclick="calculator.display.value += '9'"></td>

      <td><input type='button' name='equals' value='=' onclick="calculator.display.value += eval(calculator.display.value)"></td>

      <td><input type='button' id='clear' name='clear' value='c' onclick="calculator.display.value += ' '"></td>
    </tr>
  </table>
</form>

穆罕默德·萨尔曼(Muhammad Salman)

你做错了基本上是这样的:

<td><input type='button' name='equals' value='=' onclick="calculator.display.value += eval(calculator.display.value)"></td>

<td><input type='button' id='clear' name='clear' value='c' onclick="calculator.display.value += ' '"></td>

在这两种情况下,您都将添加到当前值。

结果和清除按钮不应将其应设置为等于的值相加,因此变为:

<td><input type='button' name='equals' value='=' onclick="calculator.display.value = eval(calculator.display.value)"></td>

<td><input type='button' id='clear' name='clear' value='c' onclick="calculator.display.value = ' '"></td>

我所做的就是删除两个字符,它们+calculator.display.value

您使用的是calculator.display.value +=我将其更改为calculator.display.value =


本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章