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

时间:2021-02-22 16:53:52

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

我猜脚本行出了问题它对英语标签很有效,但对多字节语言如波斯文就不行

4 个解决方案

#1


3  

Probably line 212 in the TextboxList.Autocomplete.js is to blame:

可能是TextboxList.Autocomplete中的第212行。js是罪魁祸首之一。

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 _. Because RegExp knows nothing about Unicode this won't work where the word begins with a non-ASCII character.

那就是在一个词的边界后寻找给定的字符。但是单词边界依赖于对单词字符的识别,JavaScript RegExp的单词字符列表只是ASCII字母数字加_。因为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,在这种情况下,它会将任何建议与给定的字符串匹配,而不仅仅是在单词的开头。

#2


1  

Your HTTP header is wrong. It should be:

您的HTTP报头错误。应该是:

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);

#3


1  

Your content-type header is somewhat wrong. First, it should be content-type:something; charset=something, that is, content-type:text/html; charset=utf-8.

您的内容类型头有点错误。首先,它应该是content-type:something;charset =一些,内容类型: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?

但是实际上建议使用content-type application/json,看看这里正确的json内容类型是什么?

So, you could do it like this

你可以这样做

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

#4


1  

Probably line 215 in the TextboxList.Autocomplete.js is to blame:

可能是在TextboxList.Autocomplete的第215行。js是罪魁祸首之一。

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行。

#1


3  

Probably line 212 in the TextboxList.Autocomplete.js is to blame:

可能是TextboxList.Autocomplete中的第212行。js是罪魁祸首之一。

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 _. Because RegExp knows nothing about Unicode this won't work where the word begins with a non-ASCII character.

那就是在一个词的边界后寻找给定的字符。但是单词边界依赖于对单词字符的识别,JavaScript RegExp的单词字符列表只是ASCII字母数字加_。因为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,在这种情况下,它会将任何建议与给定的字符串匹配,而不仅仅是在单词的开头。

#2


1  

Your HTTP header is wrong. It should be:

您的HTTP报头错误。应该是:

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);

#3


1  

Your content-type header is somewhat wrong. First, it should be content-type:something; charset=something, that is, content-type:text/html; charset=utf-8.

您的内容类型头有点错误。首先,它应该是content-type:something;charset =一些,内容类型: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?

但是实际上建议使用content-type application/json,看看这里正确的json内容类型是什么?

So, you could do it like this

你可以这样做

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

#4


1  

Probably line 215 in the TextboxList.Autocomplete.js is to blame:

可能是在TextboxList.Autocomplete的第215行。js是罪魁祸首之一。

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行。