jquery - 如何使jquery计算器刚刚执行计算后不追加数字?

标签 jquery append reset digits

我的任务是在 jquery 中创建一个计算器,在我从该网站的用户那里得到的答案的帮助下,我几乎使它能够正常工作。剩下的问题之一是,每次执行计算后(例如:2+5 =7),之后推送的数字都会 append 到结果中,而我希望在按下新按钮时自动重置最终值已显示。 这是代码:

var $calc = $("#calculator");
            var $display = $("#display");
            var $numbers = $("#numbers button");
            var $operators = $("#operators button");
            var $colspan = $(".colspan");

            var init = 0;
            var operand = init;
            var operation = null; 
            var afterOperation = false;

            var reset = function() {
                $display.text(init);
                operand = null;
                operation = null;
            }

            $numbers.not($('#colspan')).click(function(event) {
                event.preventDefault();
                var currValue = parseFloat($display.text());
                var currValue = parseFloat(operand) + parseFloat(operation);
                  if($("#display").data("currValue") == true) { 
                    reset($(this).text()); 
                    }
                if(afterOperation == false) {
                    if($display.text() == init && $display.text().indexOf(',')<0) {
                        $display.text($(this).text());
                    } else {
                    $display.append($(this).text());
                    } 
                } else {
                    $display.text($(this).text());
                    afterOperation = false;
                }
            });
            $operators.click(function(event) {
                event.preventDefault();
                var currValue = $(this).text();
                var currOperand = parseFloat($display.text());
                if(currValue != 'CE') {
                if(operation != null && !afterOperation){
                    switch(operation) {
                        case '+':
                            $display.text(operand + currOperand);
                            break;
                        case '-':
                            $display.text(operand - currOperand);
                            break;
                        case '*':
                            $display.text(operand * currOperand);
                            break;
                        case '/':
                            if(currOperand != 0) {
                                $display.text((Math.round(operand / currOperand * 100)/100).toString().replace(".",","));
                            } else {
                                alert("ERROR");
                                reset();
                            }
                            break;
                        }   
                    }
                }
                if(currValue != '=') {
                        operation = currValue;
                } else {
                        operation = null;
                        afterOperation = false;
                        return;
                }
                if(currValue != 'C') {
                   operation = currValue;
                } else {
                        $display.text(init);
                }   
                if(currValue != '←') {
                        operation = currValue;
                } else {
                        $display.text($display.text().substring(0,$display.text().length-1));
                }
                if(operand = parseFloat($display.text())) {
                        afterOperation = true;
                } else {
                    reset();
                }

            });
    });

我不确定问题出在哪里,因为我尝试在 $numbers.not- 以及 $operators 函数中应用重置变量。但它并没有达到目的。有什么建议吗?

最佳答案

你能做的事情有点棘手。当您使用以下方法添加结果时,将类添加到 $display:

$display.text(operand + currOperand).addClass('result');

单击每个按钮时,检查 $display 是否具有类结果,然后使用以下方法清除字段:

if($display.hasClass('result')){
    $display.val('');
}

希望您了解可以做什么。我没有你的 HTML 代码,所以可能我不准确。

关于jquery - 如何使jquery计算器刚刚执行计算后不追加数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30668270/

相关文章:

php - Ajax/jquery 登录表单失败

python - 一次向列表中的两个元素添加一个元素

javascript - jquery - 将for循环生成的图标 append 到列表项 anchor 中

git - 重置 --hard git 远程分支到远程 reflog 中的 SHA 但不在本地 repo 中

javascript - 按标签名称向所有文档子级添加事件监听器

javascript - 模块模式 JavaScript - 滚动事件不起作用

asp.net-mvc - Web API ajax 调用中参数为 null

python - 结果为 append 到列表的多处理 for 循环

javascript - ajax 提交后清除表单值。

css - 为特定的 div 重置 css