jQuery Jquery禁用键盘键

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

Jquery disabling a keyboard key

jqueryhtml

提问by Lvcky Mercado

Is it possible to disable a certain keyboard key (like asterisk or string) by using jquery?

是否可以使用 jquery 禁用某个键盘键(如星号或字符串)?

回答by detheridge02

You can't disable a key stroke as such but you could capture it with jQuery and overwrite its action (return false). The example below captures the enter key. Just change 13 to any key code you need to disable.

您不能像这样禁用击键,但您可以使用 jQuery 捕获它并覆盖其操作(返回 false)。下面的示例捕获回车键。只需将 13 更改为您需要禁用的任何键代码即可。

$("input").keypress(function (evt) {

  var keycode = evt.charCode || evt.keyCode;
  if (keycode  == 13) { //Enter key's keycode
    return false;
  }
});

回答by Ijas Ameenudeen

I had to disable all the keys but the F11 and ESC. So I did this way. Think this may help someone. :)

我不得不禁用除 F11 和 ESC 之外的所有键。所以我是这样做的。认为这可能对某人有所帮助。:)

$(document).on('keydown',function(e)
{ 
    var key = e.charCode || e.keyCode;
    if(key == 122 || key == 27 )
        {}
    else
        e.preventDefault();
});

回答by jere

i can think of something like this:

我可以想到这样的事情:

// Bind this keypress function to the html (not sure about this) or body tags of your page
$("body").keypress(function (evt) {
//Determine where the character code is coming from
var charCode = evt.charCode || evt.keyCode;

if (charCode  == 13) { //Enter key's keycode, could be any other key
return false;
}
});

by returning false you are telling the browser to not allow the key pressed event

通过返回 false 你告诉浏览器不允许按键事件

回答by Amadan

Handle onKeyDown, then preventDefaulton it.

处理onKeyDown,然后preventDefault就可以了。

回答by mas-designs

Just add an OnKeyUp,Down-EventListenerto your HTML Page and if the pressed key is that you want to ignore you just preventDefault

只需添加OnKeyUp,Down-EventListener到您的 HTML 页面,如果按下的键是您想忽略您preventDefault

回答by dov.amir

you can use the following plugin like this : $('#text_input').filter_input({regex:'[a-z]'});

您可以使用下面的插件是这样的: $('#text_input').filter_input({regex:'[a-z]'});

http://www.thimbleopensource.com/tutorials-snippets/jquery-plugin-filter-text-input

http://www.thimbleopensource.com/tutorials-snippets/jquery-plugin-filter-text-input