javascript – 在快速键入时,.keyup()上的非拉丁字符替换不起作用

我有jQuery代码用拉丁字符替换Cyrillic用户输入.当用户输入速度不是太快(每分钟60-70个字符)时这个工作正常,但是当用户打字速度快于此时,它混合了西里尔字符和拉丁字符,可能是因为无法及时捕获键盘事件.我怎样才能解决这个问题?

$("input[name*=Name]").not("input[name*=Main]").keyup(function (e) {
            if (e.keyCode === 8 || e.keyCode === 46 || e.keyCode === 9 || e.keyCode === 13 || e.keyCode === 17 || e.keyCode === 18 || e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 39 || e.keyCode === 40 || e.keyCode === 16 || e.keyCode === 20) {
                return false;
            } else {
                var englishchars = ["a", "b", "v", "g", "d", "e", "yo", "zh", "z", "i", "yi", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "f", "kh", "c", "ch", "sh", "shch", "i", "e", "yu", "ya", ""];
                var slavicchars = ["а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ы", "э", "ю", "я", "ь"];
                var verified = String.fromCharCode(e.wich).match(/[^а-яё]/);
                if (verified) {
                    e.preventDefault();
                    var lastchar = this.value.length > 1 ? this.value.substr(-1) : this.value;
                    var russiancharindex = jQuery.inArray(lastchar, slavicchars);
                    if (russiancharindex > 0) {
                        var englishcharindex = englishchars[russiancharindex];
                        this.value = this.value.replace(lastchar, englishcharindex);
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        });

最佳答案

(…) probably because cannot catch keyup event in time (…)

这是因为当释放键时会捕获keyup事件,而当按下键时字符被插入到文本框中.因此,短按两个键(快速键入),两个字符已经存在于文本框中,然后释放键一个接一个,你只检查输入值的最后一个字符,但第一个释放的密钥被发送到最后一个字符之前的一个字符.

var lastchar = this.value.length > 1 ? this.value.substr(-1) : this.value;
var russiancharindex = jQuery.inArray(lastchar, slavicchars);
// ...

也是因为这个原因,当您向后移动光标以在文本中间插入一个字母时,它将无法工作.

这个怎么样?

在每个keyup上它将匹配字符串中的每个非ascii字符(而不是仅匹配最后一个字符).注意.更新输入值将导致光标丢失其当前位置,并将在输入结束时移动.为了防止这种情况(如果您在中间某处编辑字段),请在更新之前存储其位置并在此之后恢复.

(缩短了你的if(e.keyCode …)条件并为keyCodes添加了空格[32])

// keep these variables outside of the handler, so that they're not redeclared unnecessarily:
var charsObject = {
    'a' : 'а',
    'б' : 'b',
    'в' : 'v',
    // ...
}, keyCodes = [8,46,9,13,17,18,32,37,38,39,40,16,20];

$("input[name*=Name]:not([name*=Main])").keyup(function(e){
    if (keyCodes.indexOf( e.keyCode ) < 0) {
        // store current cursor position:
        var start = this.selectionStart, end = this.selectionEnd;
        $(this).val($(this).val().replace(/[^\u0000-\u007f]/g, function(char, key) {
            // return replacement letter, or the original letter if it's not a "charsObject" key:
            return charsObject[char] || char;
        }));
        // restore cursor position after field update:
        this.setSelectionRange(start, end);
    }
});

JSFiddle

点赞