计算2个ember.js属性的和

Calculate sum of 2 ember.js properties

本文关键字:属性 js 2个 ember 计算      更新时间:2024-02-24

我有一个函数,它有两个参数,它们是Ember对象的属性,如下所示:

mathOp: function(){
        if (this.get('operator') == '+'){
            var result = this.get('opr1') + this.get('opr2');
            alert(result);
        }
}

我输入的是opr1=12和opr=23(例如)。相加是将它们作为字符串相加,即结果=1223。如何使它们正常相加,即使结果=35?请帮忙。

您可以使用函数parseInt将字符串转换为数字。

mathOp: function(){
        if (this.get('operator') == '+'){
            var result = parseInt(this.get('opr1'), 10) + parseInt(this.get('opr2'), 10);
            alert(result);
        }
}