如何在不使用 ALT+SHIFT 的情况下将 select2 输入语言更改为波斯语

问题描述 投票:0回答:0

我在一个网站上工作,用户必须用不同的语言填写字段。我不希望用户使用 Alt+Shift 键更改他们的书写语言,但我希望输入语言自动更改为 perisan 或英语。 现在,在您的帮助下,我可以使用以下代码在文本上执行此操作,但我无法在 select2 字段的搜索部分执行此操作,但一位伊朗程序员已在他的网站上执行此操作。

你能指导我吗

<script>
            var persianCharacters={ A: "ا", B: "ب", C: "ک", D: "د", E: "ه", F: "ف", G: "گ", H: "ح", I: "ی", J: "ج", K: "ک", L: "ل", M: "م", N: "ن", O: "و", P: "پ", Q: "ق", R: "ر", S: "س", T: "ت", U: "ی", V: "و", W: "و", X: "ط", Y: "ی", Z: "ز", a: "ا", b: "ب", c: "ک", d: "د", e: "ه", f: "ف", g: "گ", h: "ح", i: "ی", j: "ج", k: "ک", l: "ل", m: "م", n: "ن", o: "و", p: "پ", q: "ق", r: "ر", s: "س", t: "ت", u: "ی", v: "و", w: "و", x: "ک", y: "ی", z: "ز",};
            var hotkeys = {'Shift+a': function(){ console.log('Shift+a pressed');}, 'Ctrl+b': function(){ console.log('Ctrl+b pressed');}, 'Alt+c': function(){ console.log('Alt+c pressed');}};

            var inputs = document.querySelectorAll("input[type='text'], input[type='search'], .select2-search__field");
            inputs.forEach(function(input) 
            {
                input.addEventListener("keydown", function (event) 
                {
                    console.log(event.key);
                    var key = event.key;
                    if (event.key === "Backspace") 
                    {
                        var text = event.target.value;
                        var caretPos = event.target.selectionStart;
                        var newText = text.slice(0, caretPos - 1) + text.slice(caretPos);
                        event.target.value = newText;
                        event.target.selectionStart = caretPos - 1;
                        event.target.selectionEnd = caretPos - 1;
                        event.preventDefault();
                    }
                    else if (event.key === "Delete") 
                    {
                        var text = event.target.value;
                        var caretPos = event.target.selectionStart;
                        var newText = text.slice(0, caretPos) + text.slice(caretPos + 1);
                        event.target.value = newText;
                        event.target.selectionStart = caretPos;
                        event.target.selectionEnd = caretPos;
                        event.preventDefault();
                    }
                    else if (event.altKey) 
                    {
                        event.preventDefault();
                    }
                    else if (event.ctrlKey) 
                    {
                        event.preventDefault();
                    }
                    else if (/^[a-zA-Z]*$/.test(key)) 
                    {
                        var persianChar = persianCharacters[key.toUpperCase()];
                        if (event.shiftKey) 
                        {
                            persianChar = persianChar.toUpperCase();
                        }
                        event.preventDefault();
                        input.value += persianChar;
                    }
                });
            });   
        </script>

我想在不更改键盘语言的情况下在 select2 搜索字段中输入波斯语

javascript html jquery-select2-4
© www.soinside.com 2019 - 2024. All rights reserved.