【发布时间】:2023-03-27 01:19:01
【问题描述】:
我在 jquery 的自动完成中遇到重音问题。 当我输入带有重音的字母时,我得到的结果类似于“M& Aacute;THEUS”而不是“MÁTHEUS” 我尝试将 utf8_encode 更改为解码,然后我有“M?THEUS”
按照下面我的代码进行操作
retornar_cliente_processo.php
<?php require_once("conexao/conexao.php"); ?>
<?php
$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends
$qstring = "SELECT clienteNome as value,clienteId as id FROM cliente WHERE clienteNome LIKE '%".$term."%' LIMIT 10";
$consulta_tr = mysqli_query($conecta, $qstring);
if(!$consulta_tr) {
die("erro no banco1");
}
while ($row = mysqli_fetch_array($consulta_tr,MYSQL_ASSOC))//loop through the retrieved values
{
$row['value']=htmlentities(stripslashes(utf8_encode($row['value'])));
$row['id']=(int)$row['id'];
$row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data
?>
js
$(document).ready(function() {
$('#clientes').autocomplete({
source: 'php/retornar_cliente_processo.php',
minLength: 1,
select: function(event, ui) {
$('#clienteId').val(ui.item.id);
$('.form-control').removeAttr("disabled");
$('#clientes').attr("disabled", "disabled");
$('#alteraNome').removeAttr("disabled");
},
}); }
html
<input type="hidden" name="clienteId" id="clienteId" placeholder="ID">
<div class="form-group">
<div class="col-md-12">
<label for="clienteNome" class="control-label">Nome do cliente</label>
<div class="input-group">
<input type="text" autocomplete="off" name="clienteNome" id="clientes" class="form-control" placeholder="Nome do cliente" required>
</div>
</div>
</div>
【问题讨论】:
-
您是否看过/考虑过本示例中描述的重音折叠? jqueryui.com/autocomplete/#folding 我认为最终的结果更可取。否则,这将有助于了解编码问题发生在哪里。是回发到服务器的时候还是查询数据库的时候?
-
utf8_encode(),ISO-8859-1/Latin-1 到 UTF-8。你确定是 Latin-1 的编码吗?
标签: php jquery mysql autocomplete