简体   繁体   English

清除Javascript / HTML计算器中的值

[英]Clearing values in a Javascript/HTML calculator

Why won't my calculator clear when I press "c" or "="? 为什么当我按“ 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> 

+= concatenates the existing value with your string. +=将现有值与您的字符串连接在一起 If you want to clear the value (or set it to something brand-new), you should use = instead. 如果要清除该值(或将其设置为全新的值),则应使用=代替。

 <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> 

But inline event handlers are essentially eval inside HTML markup - they're bad practice and result in poorly factored, hard-to-manage code. 但是,内联事件处理程序本质上是eval HTML标记内-他们是不好的做法,并导致不良因素,难以管理的代码。 Seriously consider attaching your events with JavaScript, instead. 认真考虑使用JavaScript附加事件。 For example, for those last two buttons: 例如,对于最后两个按钮:

 const equals = document.querySelector('[name="equals"]'); const clear = document.querySelector('#clear'); equals.onclick = () => calculator.display.value = eval(calculator.display.value); clear.onclick = () => calculator.display.value = ''; 
  <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='='></td> <td><input type='button' id='clear' name='clear' value='c'></td> </tr> </table> </form> 

You're adding the answer onto the end of the problem with += . 您可以使用+=将答案添加到问题的末尾。 Just get rid of the + 只要摆脱+

Like this: 像这样:

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

See here: https://jsfiddle.net/odqjg5md/ 看到这里: https : //jsfiddle.net/odqjg5md/

What you are doing wrong is basically this : 你做错了基本上是这样的:

<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>

In both these cases you are adding to the current value. 在这两种情况下,您都将添加到当前值。

The result and clear button should not add the value they should set it equal to something so this becomes : 结果和清除按钮不应将其应设置为等于的值相加,因此变为:

<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>

All I did was remove two chars and they were + in calculator.display.value 我所做的只是删除两个字符和他们+calculator.display.value

You were using calculator.display.value += I changed it to calculator.display.value = 您正在使用calculator.display.value +=我将其更改为calculator.display.value =


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM