如何从 jQuery 中的事件对象获取按键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/874158/
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
How to get the keypress value from an event object in jQuery
提问by Jon
I want to disable users from entering a character into a HTML text input field that isnt a letter or number.
我想禁止用户将字符输入到不是字母或数字的 HTML 文本输入字段中。
This exact functionality can be seen on the Twitter signup page (username field) https://twitter.com/signup.
这个确切的功能可以在 Twitter 注册页面(用户名字段)https://twitter.com/signup上看到。
Im trying to write this in jQuery. But i need to be able to grab the incomming keypress event value to test which key was pressed and if its value is acceptable.
我试图用 jQuery 写这个。但是我需要能够获取传入的按键事件值来测试按下了哪个键以及它的值是否可以接受。
Here's the code:
这是代码:
<input type="text" id="username" name="username" />
<script type="text/javascript">
$("#username").keypress(function(event){
// Get the keypress value
// ...?
// If the keypress value is illegal then disable it
if (...){
event.preventDefault();
}
});
</script>
回答by Vini
You can use the 'which' property of the event object to capture the keycode. You can use something like following code to get the corresponding character -
您可以使用事件对象的“which”属性来捕获键码。您可以使用类似以下代码的内容来获取相应的字符 -
var c = String.fromCharCode(e.which);
See the link http://api.jquery.com/keypress/for detailed explanation.
有关详细说明,请参阅链接http://api.jquery.com/keypress/。
回答by Casey
When a user presses a key the corresponding ascii code is what is sent in the event. Use the e.which field
当用户按下一个键时,相应的 ascii 代码就是事件中发送的内容。使用 e.which 字段
So your conditional could look something like this:
所以你的条件可能看起来像这样:
if ( e.which > 47 && e.which < 58)
{
// it is a number
}
There are many different ways you could write the conditional. Possibly by using mapping the allowed characters into an array.
有许多不同的方法可以编写条件。可能通过使用将允许的字符映射到数组中。
For reference, Uppercase: 65 - 90 Lowercase: 97 - 122
作为参考,大写:65 - 90 小写:97 - 122
You should realize that this will make your app only work for people typing in English using the standard ASCII set. International users will be unable to write in your form field, because those keycodes fall outside the standard ASCII range and will be using a different encoding. Just something to consider.
您应该意识到这将使您的应用程序仅适用于使用标准 ASCII 集输入英语的人。国际用户将无法在您的表单域中书写,因为这些键码不在标准 ASCII 范围内并且将使用不同的编码。只是需要考虑的事情。
Ascii Codes: http://www.petefreitag.com/cheatsheets/ascii-codes/
Ascii 代码:http://www.petefreitag.com/cheatsheets/ascii-codes/
回答by Mary Daisy Sanchez
var ako ='';
var novalue = '';
jQuery('html').keypress(function(event){
var valuekey = event.charCode;
ako = String.fromCharCode(event.which);
jQuery('.a').each(function(){
var cn = jQuery(this).html();
if(ako == cn){
jQuery(this).hide('explode',{},'10000').remove();
}
novalue +=cn;
});
//alert(novalue);
jQuery("#cn").animate({width:'400px',border:'1px solid pink'},'slow');
});
TRY THIS ONE
试试这个