【发布时间】:2018-03-16 04:41:34
【问题描述】:
我有一个输入字段的映射列表:
<FormControl
name={row_index}
value={barcode.barcode}
placeholder="Barcode - then Enter"
onChange={this.onChange}
onKeyPress={this._handleKeyPress}
disabled={barcode.submitted}
/>
我目前正在使用onKeyPress 处理提交:
_handleKeyPress = (e) => {
if (e.key === 'Enter') {
const name = e.target.name;
const barcodes = this.state.barcodes;
const this_barcode = barcodes[name];
let apiFormatted = {"barcode": this_barcode.barcode, "uid": this.props.currentSession}
this.postBarcodeAPI(apiFormatted, name)
}
}
在当前输入字段成功提交后,我正在尝试关注 下一个 输入字段。 React 文档 has an example 用于使用 ref={(input) => { this.textInput = input; }} /> 手动将焦点设置在单个输入字段上。我尝试过使用this[‘textInput’+‘1’].focus()(使用computed property names,但我收到一个错误,即函数无效。
编辑
根据 Chase 的回答,我链接到自动对焦文档 although it doesn't work in this case。
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/autofocus
【问题讨论】:
-
我的答案不再起作用了吗?看起来它已作为答案被删除。
-
部分不行,看我的评论。但我重新接受了替代答案。