-5

私はjQueryを楽しんでいて、基本的な加算と減算の計算機を作成しようとしていますが、ユーザーが数値を加算(または減算)すると、入力ボックスの以前の数値が消えずに追加されるときに問題が発生します。次の関数。

どうすれば修正できますか?

これがフィドルです:http://jsfiddle.net/maniator/8v9zT/7/

これが私のjavascriptコードです:

var calculator = {
    calcArea: $('<div>',{id: 'calcArea'}),
    buttons: $('<div>',{id: 'buttonArea'}),
    textArea: $('<input>',{type: 'text', id: 'calcText', readonly: true}),
    calcStack: null,
    prevEq: null,
    body: $('body'),
    init: function(){
        var self = this;
        self.createInterface();
        $('button').click(function(e){
            self.clickButton(e.currentTarget)
        })
    },
    createInterface: function(){

        this.buttons.append(this.textArea);

        for(var i = 1; i <= 10; i++){
            this.buttons.append($('<button>',{text: (i==10?0:i), class: 'button'}))
        } 

        this.buttons.append($('<button>',{text: '+', class: 'button'}))
        this.buttons.append($('<button>',{text: '-', class: 'button'}))
        this.calcArea.append(this.buttons);
        this.body.append(this.calcArea);
    },
    clickButton: function(obj){
        var html = obj.innerHTML;
        var operator = false;

        if(html == '+' || html == '-'){
            if(this.calcStack == '+' || this.calcStack == '-' || this.calcStack == null){
                alert('error cannot do that!');
                return;
            }
            operator = true;
        }
        if(this.calcStack == '+'){
            html = parseInt(html) + parseInt(this.prevEq);
            operator = true;
            console.log('adding')
        }
        if(this.calcStack == '-'){
            html = parseInt(html) - parseInt(this.prevEq);
            operator = true;
            console.log('subtracting')
        }
        this.prevEq = this.calcStack;
        this.calcStack = (operator?html:((this.calcStack!=null?this.calcStack:'') + html));
        console.log('you clicked:', html, this.prevEq);
        this.textArea.val(this.calcStack)
    }
} 

calculator.init();

どうもありがとうございました^_^

4

2 に答える 2

1

やったほうがいい

html =  parseInt(this.prevEq) + parseInt(html);

html =  parseInt(this.prevEq) - parseInt(html);

それ以外の

html =  parseInt(html) + parseInt(this.prevEq);

html =  parseInt(html) - parseInt(this.prevEq);

追加するときは、順序は問題ありません。それはあなたが引き算しているときです。(a + b) == (b + a)しかし(a - b) != (b - a)

編集

おっと、これはあなたが解決したかった問題ではないようです。私はあなたのコードを調べて、最初に見たバグを解決しました。結果を表示し、私が推測するスタックをクリアするには、「=」ボタンを追加する必要があります。あなたがここで何をしようとしているのかよくわかりません。私には、数字を足したり引いたりし続けることは完全に受け入れられるようです。

編集2

最初からやり直して、新しいソリューションを思いついた

于 2011-03-30T19:31:11.210 に答える
0

個人的にはこのようにします:(ここでフィドル:http://jsfiddle.net/billymoon/QWUhW/

// declare global variables
var result = 0, current = '', adding = true

// create calculator interface
$('body').append(
    $('<div />').attr({ id: 'calcArea' }).append(
        $('<p />').attr({ id: 'calcText' }).html(result), $('<div />').attr({ id: 'buttonArea' }).append(
            $('<button />').addClass('button').text(7),
            $('<button />').addClass('button').text(8),
            $('<button />').addClass('button').text(9),
            $('<button />').addClass('button').text(4),
            $('<button />').addClass('button').text(5),
            $('<button />').addClass('button').text(6),
            $('<button />').addClass('button').text(1),
            $('<button />').addClass('button').text(2),
            $('<button />').addClass('button').text(3),
            $('<button />').addClass('button').text(0),
            $('<button />').addClass('button').text('+'),
            $('<button />').addClass('button').text('-')
        )
    )
)

// add click events
$('button').click(function() {

    // if it is a plus or minus
    if (m = $(this).text().match(/([\+\-])/)){

        // if mode is adding (+ was pressed more recently than -)
        // add result = result + current
        // otherwise result = result - current
        result = adding ? result + parseInt(current) : result - parseInt(current)

        // display result in results area
        $('#calcText').text(result)

        // reset current value
        current = ''

        // if + was pressed, adding is true else adding is false
        adding = m[1] == '+' ? true : false

    } else { // otherwise it is a number

        // add to current (as a string)
        current = current + $(this).text()

        // display current in results area
        $('#calcText').text(current)
    }
})
于 2011-03-30T20:05:36.510 に答える