[英]How to focus on a Material UI Button while Enter key pressing
after the last inputing finished, user want to press save button最后一次输入完成后,用户想按保存按钮
'Tab' key will move the focus to the next element(button), I want use 'Enter' key realize this, but i did not how to do that? 'Tab'键将焦点移动到下一个元素(按钮),我想用'Enter'键实现这一点,但我没有怎么做?
You need to use ref
to focus the button when press Enter in the last input:在最后一个输入中按 Enter 时,您需要使用ref
来聚焦按钮:
Firest, create ref: Firest,创建参考:
const buttonRef = useRef(null);
Then, add ref in the button:然后,在按钮中添加 ref:
<button ref={buttonRef} ...>
After that, add onKeyPress
in the last input to check press Enter
:之后,在最后一个输入中添加onKeyPress
以检查按Enter
:
<input onKeyPress={handleKeyPress} ...>
Anh this is the handleKeyPress
: Anh 这是handleKeyPress
:
const handleKeyPress= e => {
if(e.key === "Enter"){
buttonRef.current.focus()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.