这一定很容易,但由于我糟糕的jQuery技能,我真的陷入困境,无法解决问题。
目标是在用户在输入字段中键入时将拉丁字符转换为大写希腊字符。
似乎我非常接近,但我的问题是我将拉丁语和希腊语两个字符组合在一起用于单个键盘事件。这是我的小提琴https://jsfiddle.net/harman79/dymtufy8/
HTML:
<input type="search" id="search-id">
JQUERY:
$("#search-id").bind('keyup', function(e) {
var charMap = {
65: 913,
66: 914,
67: 936,
68: 916,
69: 917,
70: 934,
71: 915,
72: 919,
73: 921,
74: 926,
75: 922,
76: 923,
77: 924,
78: 925,
79: 927,
80: 928,
81: 931,
82: 929,
83: 931,
84: 932,
85: 920,
86: 937,
87: 937,
88: 935,
89: 933,
90: 918
};
$.each(charMap, function(index, value) {
if (index == e.which) {
$("#search-id").val(function(index, val) {
return val + String.fromCharCode(value);
});
}
});
});
如果我用return val + String.fromCharCode(value);
替换return String.fromCharCode(value);
我只会获得转换后的字符,但每个新的密钥都会替换以前的输入内容。
任何肝脏都会非常感激! 哈利
答案 0 :(得分:1)
你可以试试这个:
$("#search-id").bind('keyup', function(e) {
var charMap = {
65: 913,
66: 914,
67: 936,
68: 916,
69: 917,
70: 934,
71: 915,
72: 919,
73: 921,
74: 926,
75: 922,
76: 923,
77: 924,
78: 925,
79: 927,
80: 928,
81: 931,
82: 929,
83: 931,
84: 932,
85: 920,
86: 937,
87: 937,
88: 935,
89: 933,
90: 918
};
$.each(charMap, function(index, value) {
if (index == e.which) {
$("#search-id").val(function(index, val) {
// Remove last character and replace with new value
return val.substring(0, val.length - 1) + String.fromCharCode(value);
});
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="search" id="search-id">