主要问题是,即使使用拉丁语键盘,我也想在输入中写入格鲁吉亚字符。我的代码是这样的:在keydown上,它获取按下的键的char,然后找到它的格鲁吉亚等价物,并将char添加到字段中的值。代码工作正常但是,当涉及到输入的边缘时,字符串不会移动,我无法看到更新的值。我试着把注意力集中在关键字的输入上,但这并没有帮助(因为它已经集中注意力);你有任何想法如何解决它?在此先感谢您的帮助。
var aaa = {};
aaa["a"] = "0x10d0";
aaa["b"] = "0x10d1";
aaa["g"] = "0x10d2";
aaa["d"] = "0x10d3";
aaa["e"] = "0x10d4";
aaa["v"] = "0x10d5";
aaa["z"] = "0x10d6";
aaa["T"] = "0x10d7"; // t
aaa["i"] = "0x10d8";
aaa["k"] = "0x10d9";
aaa["l"] = "0x10da";
aaa["m"] = "0x10db";
aaa["n"] = "0x10dc";
aaa["o"] = "0x10dd";
aaa["p"] = "0x10de";
aaa["J"] = "0x10df"; // zh
aaa["r"] = "0x10e0";
aaa["s"] = "0x10e1";
aaa["t"] = "0x10e2"; // t
aaa["u"] = "0x10e3";
aaa["f"] = "0x10e4";
aaa["q"] = "0x10e5";
aaa["R"] = "0x10e6"; // gh/g
aaa["y"] = "0x10e7";
aaa["S"] = "0x10e8"; // sh
aaa["C"] = "0x10e9"; // ch
aaa["c"] = "0x10ea"; // ts
aaa["Z"] = "0x10eb"; // dz
aaa["w"] = "0x10ec"; // ts
aaa["W"] = "0x10ed"; // ch/tch
aaa["x"] = "0x10ee"; // kh
aaa["j"] = "0x10ef";
aaa["h"] = "0x10f0";
var aab = {};
for (var key in aaa) {
aab[aaa[key]]=key;
}
window.onload = function() {
var tbox_lemma = document.getElementById("tbox_lemma");
tbox_lemma.onkeypress = function(e) {
e = e || window.event;
var keycode = e.charCode || e.keyCode;
var c = String.fromCharCode(keycode);
if( c in aaa ) {
var txtToAdd = String.fromCharCode(aaa[String.fromCharCode(keycode)]);
var caretPos1 = tbox_lemma.selectionStart;
var caretPos2 = tbox_lemma.selectionEnd;
var val = tbox_lemma.value;
tbox_lemma.value = val.substring(0, caretPos1) + txtToAdd + val.substring(caretPos2);
tbox_lemma.selectionStart = caretPos1 + 1;
tbox_lemma.selectionEnd = tbox_lemma.selectionStart;
return false;
}
};
};
这是一个小提琴:http://jsfiddle.net/4dczfknx/
问题出在Chrome上。在Firefox上它工作正常。
答案 0 :(得分:1)
由于您使用jQuery标记了您的问题,请将此行添加到onload函数的末尾:
$('#tbox_lemma').blur().focus().val($('#tbox_lemma').val());