简体   繁体   中英

JQuery Calculator: Calculate Input, Clear After

http://jsbin.com/UYusECo/1/
http://jsbin.com/UYusECo/1/edit

All the functions on the calculator work fine, but after I hit equal for say 6+6= and then my result shows to be 12. When I press 2 or any other number I want to clear the input of 12 or whatever the last calculation was and start a new calculation. Can anyone help me on this?

Looking at you code, it will be a bit messy but when hitting equals set a variable to true

newCalc = true;

When catching the input if (newCalc) {$("#clear).trigger (); newCalc = false}

Here's an easier way to do it, where you don't have to edit every button. Just use this as the handler for your = key:

comp.click(function () {
    Input.val(eval(Input.val()));
    $('input:button').one('click', function newOp(e) {
        if (/\d/.test(this.value)){
            Input.val(this.value);
        }
        $('input:button').off('click', newOp);
    });
});

Not the best way to do it, certainly, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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