入力フィールドに数字だけを入力する必要があります。 onKeyPress & onKeyDownイベントを使用してみました。私はブール値のイベントを取得することができません.Anyの助けをいただければ幸いです。onKeyPressまたはonKeyDownによるReactの数字のみを許可する
class User extends Component {
constructor (props) {
super(props)
this.state = { value: ''}
this.keyPress = this.keyPress.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.setState({ value: e.target.value });
}
keyPress(e){
if(e.keyCode == 13){
console.log('value', e.target.value);
}
else{
const charCode = (e.which) ? e.which : e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)){
return false;
}
else{
return true;
}
}
}
<input
type='text'
value={this.state.value}
onKeyDown={this.keyPress(event)}
onChange={this.handleChange}
/>
ありがとう!!私は後で "XXX"のような文字列で入力をマスクする必要があるので、type = 'number'を使用することはできません。 – user9074131