#的一致keyCode [英] consistent keyCode for `#`

查看:105
本文介绍了#的一致keyCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道由于e.keyCode vs e.charCode而捕获键并不是一件容易的事,但我认为jQuery几乎能够规范大多数这些不一致之处.

While I know that capturing keys due to the e.keyCode vs e.charCode is not trivial, I thought that jQuery would pretty much be able to normalize most of those inconsistencies.

但是,当回答此问题时,我发现字符#似乎具有非常不一致的keyCodes(和当然,这对于其他一些代码也适用,主要取决于我猜想的浏览器和键盘布局.

However while answering this question I found out that the character # seems to have very inconsistent keyCodes (and of course this is true for several other codes also, mostly depending on the browser and keyboardlayout I guess).

Chrome和IE在我的计算机上生成191,Firefox 163,另一个用户报告222.Chromewindow.event甚至将U+00BF报告为keyIdentifier-根据unicode表,其应为¿.

Chrome and IE yielded 191, Firefox 163 on my computer, another user reported 222. Chromes window.event even reported U+00BF as keyIdentifier - which according to unicode tables should be ¿.

您知道采用一致的键码来确定诸如#这样的符号的任何一致方法,而无需执行以下操作:

Do you know any consistent way to determine such symbols like the # with inconsistent keyCodes without doing something nasty like the following:

$('input').keydown(function (e) {
        if (e.which == 191 || e.which == 163 || e.which == 222){
            // hope you got the right key
            e.preventDefault();
        }
});

请拨弄小提琴.

推荐答案

这在使用美国键盘的Chrome和Firefox中对我有效:

This works for me in Chrome and Firefox with a US keyboard:

$('[id$=txtClient]').keypress(function (e) {
    if (String.fromCharCode(e.which) == '#') {
        e.preventDefault();
    }
});

keypress是唯一可以为您提供输入的字符可靠信息的事件.

keypress is the only event that will give you reliable info on the character that was entered.

演示: http://jsfiddle.net/elclanrs/ebcet/9/

这篇关于#的一致keyCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆