javascript JS:在按键中识别点或删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5940992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS: Recognize dot or delete in keypress
提问by Florian Müller
I'd like to execute some code if the user presses the dot (on standard keybord or on numblock). But if I take it over Keycode (110), this is the same like the delete button.
如果用户按下点(在标准键盘上或在 numblock 上),我想执行一些代码。但是如果我把它换成 Keycode (110),这和删除按钮是一样的。
How do I recognize them?
我如何识别它们?
Thanks for your help!
谢谢你的帮助!
采纳答案by invertedSpear
Delete key (usually above arrows) is 46, numpad decimal is 110, and the keyboard period is 190.
删除键(通常在箭头上方)为 46,小键盘十进制为 110,键盘周期为 190。
This is a pretty good page to know what keycodes are what: http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
这是一个很好的页面,可以了解什么是键码:http: //www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
If this doesn't answer your question, please rephrase it as it's a little confusing what you are looking for.
如果这不能回答您的问题,请重新措辞,因为您正在寻找的内容有点令人困惑。
回答by Gibolt
Use modern JS!
使用现代JS!
Use event.key === "." || event.key === "Delete"
, rather than arbitrary number codes!
使用event.key === "." || event.key === "Delete"
, 而不是任意数字代码!
回答by Osama Bari
it's only allow dot and numbers
它只允许点和数字
const charCode = (event.which) ? event.which : event.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=46 ) {
return false;
}
return true;