简体   繁体   English

使用jquery自动完成标记的utf-8问题

[英]utf-8 problem in using jquery autocomplete tags

hey mates . 嘿伙伴。 recently i used jquery auto-complete tag 最近我使用了jquery自动完成标签

http://devthought.com/projects/jquery/textboxlist/ http://devthought.com/projects/jquery/textboxlist/

everything goes fine except utf-8 tag suggesting , only English tags are suggested 除了utf-8标签建议外,一切都很顺利,建议只提供英文标签

i guess something goes wrong with script lines it works fine with English tags but not with multi byte languages like Persian 我猜测脚本行出了问题,它可以正常使用英文标签,但不能使用像波斯语这样的多字节语言

Probably line 212 in the TextboxList.Autocomplete.js is to blame: TextboxList.Autocomplete.js中的第212行可能是罪魁祸首:

regexp = new RegExp('\\b' + escapeRegExp(search), insensitive ? 'i' : '');

That's looking for the given character after a word boundary. 那是在单词边界之后寻找给定的字符。 But word boundaries are dependent on recognition of word characters, and JavaScript RegExp's list of word characters is just the ASCII alphanumerics plus _ . 但是单词边界依赖于对单词字符的识别,而JavaScript RegExp的单词字符列表只是ASCII字母数字加_ Because RegExp knows nothing about Unicode this won't work where the word begins with a non-ASCII character. 因为RegExp对Unicode一无所知,所以在单词以非ASCII字符开头的地方不起作用。

You could try getting rid of the \\\\b in which case it would match any suggestion with the given string anywhere inside it, not just at the start of words. 您可以尝试删除\\\\b在这种情况下,它将匹配任何位于其中任何位置的给定字符串的建议,而不仅仅是在单词的开头。

Your HTTP header is wrong. 您的HTTP标头错误。 It should be: 它应该是:

header('Content-Type: application/json; charset=UTF-8');

You can also shorten your code and do the sorting with MySQL: 您还可以缩短代码并使用MySQL进行排序:

$sql = 'SELECT `tag` FROM `'.$prefix.'_tags` ORDER BY `tag`';
$result = $db->sql_query($sql);
if (!$result) {
    header($_SERVER['SERVER_PROTOCOL'].' 500 Internal Server Error');
    echo mysql_error();
    exit;
}

$response = array();
$i = 0;
while ($row = $db->sql_fetchrow($result)) {
    $response[] = array($i++, $row);
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($response);

Your content-type header is somewhat wrong. 你的内容类型标题有点错误。 First, it should be content-type:something; charset=something 首先,它应该是content-type:something; charset=something content-type:something; charset=something , that is, content-type:text/html; charset=utf-8 content-type:something; charset=something ,即content-type:text/html; charset=utf-8 content-type:text/html; charset=utf-8 . content-type:text/html; charset=utf-8

But it is actually suggested to use content-type application/json , see here What is the correct JSON content type? 但实际上建议使用内容类型的application/json ,请参阅此处什么是正确的JSON内容类型?

So, you could do it like this 所以,你可以这样做

header("Content-Type:application/json; charset=UTF-8");

Probably line 215 in the TextboxList.Autocomplete.js is to blame: TextboxList.Autocomplete.js中的第215行可能是罪魁祸首:

if (regexp.test(values[i][1])) newvals.push(values[i]);

covert it to 把它转交给

if (values[i][1].indexOf(escapeRegExp(search)) != -1) newvals.push(values[i]);

Bobince (first answer) right, the problem is in line 212, but resolved it can be in line 215 Bobince(第一个答案)对,问题在第212行,但解决了它可以在第215行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM