vuejs 方法中的 Parseint

ara*_*she 2 javascript vue.js vuejs2

例如让我们this.last取 5 和this.current60 ,我希望this.last+this.current为 65 而不是 60 5。我尝试过parseInt(this.last + this.current),但不起作用

<button class="plus" @click="plus">+</button>
<input class="res" v-model="current" maxlength="24" disabled>
<input class="res2" v-model="last" maxlength="24" disabled>
Run Code Online (Sandbox Code Playgroud)
button{
width:200px;
height:200px;
}
Run Code Online (Sandbox Code Playgroud)
data:{
  last: null,
  current: " ",
  plusClicked: false
}
methods:{
  plus:function(){
   this.last = this.current;
   this.current = " ";
   this.plusClicked = true;
  },
equal:function(){
  if(this.plusClicked == true){
   alert(parseInt(this.last + this.current));
   this.plusClicked = !true;
  }
}},
Run Code Online (Sandbox Code Playgroud)

thi*_*utg 6

首先,不要初始化this.current为," "而是初始化为0

您还应该将输入字段的类型更改为<input type="number">

要添加两个数字,请使用:

  • parseInt(this.last) + parseInt(this.current)