计算 2 ember.js 个属性的总和
Calculate sum of 2 ember.js properties
我有一个函数,它有 2 个参数,它们是 Ember 对象的属性,如下所示:
mathOp: function(){
if (this.get('operator') == '+'){
var result = this.get('opr1') + this.get('opr2');
alert(result);
}
}
我正在输入 opr1 = 12,并且 opr=23(例如)。添加是将它们作为字符串添加,即 result = 1223。如何让它们正常添加,即 make result = 35?请帮忙
您可以使用函数 parseInt
将字符串转换为数字。
mathOp: function(){
if (this.get('operator') == '+'){
var result = parseInt(this.get('opr1'), 10) + parseInt(this.get('opr2'), 10);
alert(result);
}
}
我有一个函数,它有 2 个参数,它们是 Ember 对象的属性,如下所示:
mathOp: function(){
if (this.get('operator') == '+'){
var result = this.get('opr1') + this.get('opr2');
alert(result);
}
}
我正在输入 opr1 = 12,并且 opr=23(例如)。添加是将它们作为字符串添加,即 result = 1223。如何让它们正常添加,即 make result = 35?请帮忙
您可以使用函数 parseInt
将字符串转换为数字。
mathOp: function(){
if (this.get('operator') == '+'){
var result = parseInt(this.get('opr1'), 10) + parseInt(this.get('opr2'), 10);
alert(result);
}
}