我在 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();
非常感谢您的帮助^_^