Javascript 为什么使用 jquery keypress 事件没有检测到退格键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4418562/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:20:11  来源:igfitidea点击:

Why isn't backspace being detected using jquery keypress event?

javascriptjqueryjavascript-events

提问by Babiker

Why isn't backspace being detected using jquery keypress event?

为什么使用 jquery keypress 事件没有检测到退格键?

$("#field").keypress(function(event){alert(event.which);});

采纳答案by Pointy

The "keypress" event is (in its original IE form, and in Safari/Chrome) about an actual character being added to a text field. The backspace key clearly does not add a new character to the field value, so it causes no keypress event.

“keypress”事件(在其原始 IE 形式中,在 Safari/Chrome 中)是关于添加到文本字段的实际字符。退格键显然不会向字段值添加新字符,因此不会导致按键事件。

Firefox and Opera are a little more willy-nilly about generating typing events.

Firefox 和 Opera 在生成打字事件方面更加任性。

PPK (quirksmode) considers the IE and Webkit behaviors to be the sensible ones, and I pretty much agree.

PPK ( quirksmode)认为 IE 和 Webkit 的行为是明智的,我非常同意。

回答by mdarwi

Try using keydownand keyupfunctions instead for IE.

尝试使用keydownkeyup函数代替 IE。

Javascript e.keyCode doesn't catch Backspace/Del in IE

Javascript e.keyCode 无法在 IE 中捕获 Backspace/Del

回答by adarsh mohan

The onKeyUp alone will do all the keyboard key detection in chrome..

单独的 onKeyUp 将在 chrome 中完成所有键盘按键检测..