[英]Can't use any key after e.preventDefault() on keyDown in React
I need to stop users from typing more than 4 digits in an input.我需要阻止用户在输入中输入超过 4 位数字。 Calling
e.preventDefault()
works fine with onKeyPress
, but I don't want to use it because it's deprecated.调用
e.preventDefault()
可以与onKeyPress
一起正常工作,但我不想使用它,因为它已被弃用。 Using onKeyDown
works, but after calling e.preventDefault()
, the user can't delete the text, that input is now "frozen".使用
onKeyDown
有效,但在调用e.preventDefault()
之后,用户无法删除文本,该输入现在已“冻结”。 I understand that it's by design.我知道这是设计使然。 Is there a better way to stop the user from typing more than 4 digits but still to allow the deletion of the text?
有没有更好的方法来阻止用户输入超过 4 位数字但仍然允许删除文本?
Have you attempted adding the maxLength="4"
attribute to your input?您是否尝试过将
maxLength="4"
属性添加到您的输入中?
https://www.w3schools.com/tags/att_input_maxlength.asp https://www.w3schools.com/tags/att_input_maxlength.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.