【问题标题】:How do I make the digits not append after a calculation has just been performed in a jquery calculator?刚刚在 jquery 计算器中执行计算后,如何使数字不附加?
【发布时间】:2015-06-05 13:50:48
【问题描述】:

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

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 函数中应用重置变量。它没有成功。有什么建议吗?

【问题讨论】:

    标签: jquery append reset digits


    【解决方案1】:

    你能做的有点棘手。使用以下方法向 $display 添加结果时添加一个类:

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

    点击每个按钮时,检查$display 是否有类结果,然后使用以下命令清除该字段:

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

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

    【讨论】:

    • 您好,非常感谢您的提示。我添加了代码,它可以工作......部分。当我现在进行计算时,获取结果并单击另一个数字进行新计算 - 新单击的数字不再附加到结果中。我认为这很棒,直到我想写数字 23 并且 3 替换了 2。或者如果我想要 2457,4 现在将替换 2,5 替换 4,7 替换 5。我做错了什么?
    猜你喜欢
    • 2016-08-28
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2013-04-13
    相关资源
    最近更新 更多