【问题标题】:How can I add 1 to "1" and come up with "2" in Javascript? [duplicate]如何将 1 添加到“1”并在 Javascript 中得出“2”? [复制]
【发布时间】:2014-10-12 08:14:06
【问题描述】:

我这里有一些代码:

this.$state.transitionTo('questions.question', { testId: this.$qs.userTestId, cq: this.$qs.cq + 1 });
this.$state.transitionTo('questions.question', { testId: this.$qs.userTestId, cq: this.$qs.cq - 1 });

this.$qs.cq 的值为 1。

当我的代码针对第一种情况运行而不是将其更改为 2 时,它会将其变为“11”。我怎样才能让它做一个数字加法(秒减法)?

我确实在打字稿中定义了:

cq: number;

但似乎没有报错

【问题讨论】:

  • 如果this.$qs.cq 应该是一个数字,你应该在给它赋值时使用parseInt(),否则它是一个字符串并且+ 附加到它。

标签: javascript typescript


【解决方案1】:

用途:

pareseInt(this.$qs.cq,10) + parseInt(this.$qs.cq,10)

【讨论】:

  • 这与正确答案完全相反。
  • 如果您要建议parseInt,我建议您指定函数的第二个参数。 See why.
  • @rink.attendant.6 ,谢谢你的建议,我已经更新了。
猜你喜欢
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 2016-08-29
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
相关资源
最近更新 更多