Firefox-按键错误.不能仅使用字母输入脚本使用退格键 [英] Firefox - keypress bug. Can't use backspace with only letter input script
问题描述
我有一个输入框,我只想允许字母,连字符,空格和退格键.在chrome上一切都很好,但是在Firefox退格键(或字符代码8)上不起作用. - https://jsfiddle.net/npo7y7fr/
I've an input box that I only want to allow letters, hyphen, space and backspace. All is good on chrome but on Firefox backspace (or charcode 8) does not work. - https://jsfiddle.net/npo7y7fr/
$(document).ready(function () {
$('.textInput').keypress(function (key) {
if ((key.charCode < 97 || key.charCode > 122) && (key.charCode < 65 || key.charCode > 90) && (key.charCode != 45)) return false;
});
});
我尝试添加&& (key.charCode != 8)
还将按键更改为其他按键,例如"keydown,keyup"等...
I've tried adding && (key.charCode != 8)
also changes keypress to others like 'keydown, keyup' etc...
有人可以在Firefox(40.0.3)或我可以使用的东西上使用它吗?
Can anybody get this working in Firefox (40.0.3) or something that I can use instead?
推荐答案
由于在Mozilla中Space将发送键码32,而Backspace将发送0,所以这就是为什么它在mozilla中不起作用的原因.
Since Space will send keycode 32 and backspace will send 0 in Mozilla so that's why it is not working in mozilla.
按如下所示更改脚本
$(document).ready(function () {
$('.textInput').keypress(function (key) {
if ((key.charCode < 97 || key.charCode > 122) && (key.charCode < 65 || key.charCode > 90) && (key.charCode != 45) && (key.charCode != 32) && (key.charCode != 0) ) return false;
});
});
希望这会有所帮助.. !!
hope this helps..!!
这篇关于Firefox-按键错误.不能仅使用字母输入脚本使用退格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!