Answer the question
In order to leave comments, you need to log in
How to make automatic conversion of latin characters to cyrillic in jquery.autocomplete?
Good day.
Can you please tell me how to “pump” jquery autocomplete so that Latin characters are automatically converted to Cyrillic? Here is an example - when the user enters "vjc", "Moscow" is displayed in the results.
I added a few lines to the result script (see below), but it doesn't work - if you call the script with the q parameter , the correct results are returned, but an empty list is returned in the form.
Tell me, please, what could be the matter? Thanks in advance.
<?php<br/>
include("config.php"); <br/>
<br/>
function translit($str) <br/>
{<br/>
$tr = array(<br/>
"q"=>"й","w"=>"ц" //.. и так далее<br/>
);<br/>
return strtr($str,$tr);<br/>
}<br/>
<br/>
$q = '';<br/>
if (isset($_GET['q'])) {<br/>
<br/>
$q = strtolower($_GET['q']);<br/>
$q = iconv('UTF-8','WINDOWS-1251', $q);<br/>
$q = translit($q);<br/>
<br/>
$query = "SELECT * FROM cities WHERE name_rus LIKE '%$q%' ORDER BY name_rus ASC"; <br/>
$res = mysql_query($query);<br/>
if(mysql_num_rows($res)>0){ <br/>
while($row = mysql_fetch_assoc($res)){ <br/>
$name[] = $row['name_rus']; <br/>
}<br/>
}<br/>
<br/>
}<br/>
<br/>
if (count($items) <> 0){<br/>
foreach ($name as $curr) {<br/>
echo "$curr\n";<br/>
}<br/>
}<br/>
<br/>
if (!$q) {<br/>
return;<br/>
}<br/>
Answer the question
In order to leave comments, you need to log in
Catch the input of characters in the field itself and change the Latin alphabet to Russian on the fly (using the same Jquery), i.e. the request will immediately be in Russian, because processing will happen on the client side.
///Recognize a keystroke with an English letter and translate it into Russian.
var mapKey = {
'q' : 'q', 'w' : 'q', 'e' : 'y', 'r' : 'k', 't' : 'e', 'y' : 'n ', 'u' : 'r', 'i' : 'w', 'o' : 'u', 'p' : 'h', '[' : 'x', ']' : 'b', 'a' : 'f', 's' : 's', 'd' : 'c', 'f' : 'a', 'g' : 'p', 'h' : 'p', 'j ' : 'o', 'k' : 'l', 'l' : 'd', ';' : 'w', '\'' : 'e', 'z' : 'i', 'x' : 'h', 'c' : 's', 'v' : 'm' , 'b' : 'and', 'n' : 'm', 'm' : 'b', ',' : 'b', '.' : 'Y', 'Q' : 'Y', 'W' : 'Ts', 'E' : 'U', 'R' : 'K', 'T' : 'E', 'Y' : ' Н', 'U' : 'Г', 'I' : 'Ш', 'O' : 'Ш', 'P' : 'З', '[' : 'Х', ']' : 'b' , 'A' : 'F', 'S' : 'S', 'D' : 'B', 'F' : 'A', 'G' : 'P', 'H' : 'R', ' J' : 'O', 'K' : 'L', 'L' : 'D', ';' : 'W', '\'' : 'E', 'Z' : '?', 'X' : 'h', 'C' : 'S', 'V' : 'M', 'B' : 'N', 'N' : 'T', 'M' : 'b', ',' : 'B', '.'
: 'Yu', };
$("#idinput").on('keyup', function () {
var str = $("#idinput").val();
var r = '';
for (var i = 0; i < str. length; i++) {
r += mapKey[str.charAt(i)] || str.charAt(i);
}
// $("#idinput").val(r).trigger('keydown');
/ / Trigger if this is an input field for another plugin, e.g. autocomplete
$("#idinput").val(r);
});
///Recognize a keystroke with an English letter and translate it into Russian.
Do you want the conversion to happen unconditionally, or only if something goes wrong?
If the first is better on the client side, dig towards onkeydown with the definition of the codes of the keys pressed. If the second is to send a request to the server as is, if no results are found, do a transliteration search
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question