How to move focus on next field when enter is pressed?

后端 未结 7 922
我在风中等你
我在风中等你 2020-12-29 06:02

Can you please tell me how to move focus on to the next field when the enter key is press? I use the dform plugin (which converts JSON to a form).

I Go

7条回答
  •  佛祖请我去吃肉
    2020-12-29 06:30

    On the top-level div, add onKeyDown={this.onKeyDown.bind(this)} and add the following method (ES6) to the same class as the div:

    onKeyDown(event) {
        if (event.keyCode === 13) {
            event.preventDefault()
            const inputs =
                Array.prototype.slice.call(document.querySelectorAll("input"))
            const index =
                (inputs.indexOf(document.activeElement) + 1) % inputs.length
            const input = inputs[index]
            input.focus()
            input.select()
        }
    }
    

提交回复
热议问题