当我使用 React.js 在输入中单击 Enter 时,我想找到一种专注于下一个字段的方法
@autobind
handleKeyPress(event){
if(event.key === 'Enter'){
this.refs.email.focus();
}
}
@autobind
handleKeyPressEmail(event){
if(event.key === 'Enter'){
this.refs.zip_code.focus();
}
}
<input
onKeyPress={this.handleKeyPress}
ref = 'name'
/>
<input
onKeyPress={this.handleKeyPressEmail}
ref = 'email'
/>
<input
ref = 'zip_code'
/>
这是迄今为止我发现的最好的方法,但是我不想在每次我希望发生这种情况时通过创建一个函数来重复自己。有没有更好更干净的方法来实现这一点?