计算器jquery
我对 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();
非常感谢您的帮助^_^
I am having some fun with jQuery and I am trying to create a basic add and subtract calculator but I am having some issues when a user adds (or subtracts) a number, the previous numbers in the input box dont disappear and get added to the next function.
how do i fix it?
Here is the fiddle: http://jsfiddle.net/maniator/8v9zT/7/
here is my javascript code:
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();
Thank you so much for your help ^_^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该做
and
而不是
and
添加时顺序不是问题。就是你做减法的时候。
(a + b) == (b + a)
但(a - b) != (b - a)
编辑
哎呀,这似乎不是你的问题想解决。我刚刚查看了您的代码并解决了我看到的第一个错误。
我猜您将需要添加一个“=”按钮来显示结果并清除堆栈。我不太确定你打算在这里做什么。对我来说,不断加减数字似乎是完全可以接受的。
编辑2
我重新开始并带来了一个新的解决方案
You should do
and
Instead of
and
The order is not a problem when you're adding. It is when you're subtracting.
(a + b) == (b + a)
but(a - b) != (b - a)
EDIT
Oops, this doesn't seem the problem you wanted to resolve. I just looked in your code and solved the first bug I saw.
You will need to add an '=' button to show the result and clear the stack I guess. I'm not really sure what you intend to do here. To me it seems perfectly acceptable to keep adding and subtracting numbers.
EDIT 2
I started over and came with a new solution
就我个人而言,我会这样做:
(在这里小提琴:http://jsfiddle.net/billymoon/QWUhW/)
Personally I would do it like this:
(fiddle here: http://jsfiddle.net/billymoon/QWUhW/)