我有三个数字类型的输入字段(受控输入以接受 [0-100] 范围内的数字)。和一个文本字段来显示总数。
输入字段:
<Input type="number" defaultValue="40" min="0" max="100" onKeyPress={this.handleInput} onBlur={this.updateCode1} />
<Input type="number" defaultValue="40" min="0" max="100" onKeyPress={this.handleInput} onBlur={this.updateCode2} />
<Input type="number" defaultValue="40" min="0" max="100" onKeyPress={this.handleInput} onBlur={this.updateCode3} />
总字段:
<Text fontSize={14} weight={700}>{this.state.total}</Text>
更新代码功能:
updateCode1(value) {
this.setState({ code1: value },
() => {
this.updateTotal();
});
}
updateCode2(value) {
this.setState({ code2: value },
() => {
this.updateTotal();
});
}
updateCode3(value) {
this.setState({ code3: value },
() => {
this.updateTotal();
});
}
更新总数:
updateTotal() {
this.setState(prevState => ({
total: (prevState.code1 + prevState.code2 + prevState.code3),
}),
() => {
if (this.state.total !== 100) {
this.setState({ isTotalInvalid: true });
} else {
this.setState({ isTotalInvalid: false });
}
});
}
但它没有计算总数。有任何想法吗?谢谢!
慕仙森
元芳怎么了
相关分类