我如何在 React JS 中将数字从输入转换为 int
How i can converting numbers from input to int in react js
import React, { Component } from 'react'
export default class Ccomponent extends Component {
constructor(props) {
super(props)
this.state = {
first: 0,
second: 0,
Otv: 0
};
this.firstChange = this.firstChange.bind(this);
this.secondChange = this.secondChange.bind(this);
this.Otvet = this.Otvet.bind(this);
this.ConvertToInt = this.ConvertToInt.bind(this);
}
firstChange(event){
this.setState({
first: event.target.value
});
this.Otvet()
}
secondChange(event){
this.setState({
second: event.target.value
});
this.Otvet()
}
Otvet(event){
this.setState({
Otv: this.state.first+this.state.second
});
this.ConvertToInt();
}
ConvertToInt(event){
var a = this.state.first;
var b = this.state.second;
let v = a+b //i can try use parceInt, error - 'parceInt' is not defined no-undef
console.log(v);
}
render() {
console.log(this)
return(
<div>
<input type='number' value = {this.state.first} onChange={this.firstChange}/>
<input type='number' value = {this.state.second} onChange={this.secondChange}/>
<h3>Summa: {this.state.first}+{this.state.second}</h3>
<h1>Otvet: {this.state.Otv}</h1>
</div>
)
}}
我的输入以字符串格式将值发送到对象,如果 parceInt 不起作用,我如何将 str 转换为 int。
也许有一种方法可以获取 int 形式的值?
或者 React 是否有将字符串转换为整数的内部函数?
parceInt()
拼写错误。小错别字。拼写为 parseInt()
试试看。
例如,
let str = "123"
let num = parseInt(str)
console.log(num) // 123
import React, { Component } from 'react'
export default class Ccomponent extends Component {
constructor(props) {
super(props)
this.state = {
first: 0,
second: 0,
Otv: 0
};
this.firstChange = this.firstChange.bind(this);
this.secondChange = this.secondChange.bind(this);
this.Otvet = this.Otvet.bind(this);
this.ConvertToInt = this.ConvertToInt.bind(this);
}
firstChange(event){
this.setState({
first: event.target.value
});
this.Otvet()
}
secondChange(event){
this.setState({
second: event.target.value
});
this.Otvet()
}
Otvet(event){
this.setState({
Otv: this.state.first+this.state.second
});
this.ConvertToInt();
}
ConvertToInt(event){
var a = this.state.first;
var b = this.state.second;
let v = a+b //i can try use parceInt, error - 'parceInt' is not defined no-undef
console.log(v);
}
render() {
console.log(this)
return(
<div>
<input type='number' value = {this.state.first} onChange={this.firstChange}/>
<input type='number' value = {this.state.second} onChange={this.secondChange}/>
<h3>Summa: {this.state.first}+{this.state.second}</h3>
<h1>Otvet: {this.state.Otv}</h1>
</div>
)
}}
我的输入以字符串格式将值发送到对象,如果 parceInt 不起作用,我如何将 str 转换为 int。 也许有一种方法可以获取 int 形式的值? 或者 React 是否有将字符串转换为整数的内部函数?
parceInt()
拼写错误。小错别字。拼写为 parseInt()
试试看。
例如,
let str = "123"
let num = parseInt(str)
console.log(num) // 123