我已经使用jQuery创建了一个自动完成,它从我的mysql数据库中获取以json编码的数据。
它工作得很好,但问题是它需要4-5秒来显示建议,即使从mysql数据库中选择所有产品需要0.0008秒。下面是我的代码:
输入:
<form action="product_detail1.php" method="GET">
<input name="search" style="padding: 3px 0px 3px 30px;" type="text" id="skills" class="search1" placeholder="Cerca per codice">
<label style="position: absolute; z-index: 2; top: 4px; left: 3px; font-size: 18px;" for="skills" class="glyphicon glyphicon-search" rel="tooltip" title="search"></label>
</form>自动完成:
$(function() {
$( "#skills" ).autocomplete({
source: 'search1.php'
});
});还有search1.php文件,它从dataabse中选择数据并对其进行编码:
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
$searchTerm = $_GET['term'];
$query = $db->query("SELECT codice
FROM articoli
WHERE codice LIKE '".$searchTerm."%'
ORDER BY codice ASC");
while ($row = $query->fetch_assoc()) {
$data[] = $row['codice'];
}
echo json_encode($data);如果有更快的方法,请告诉我。我在互联网上找了一下,但没有找到way.Thanks!
发布于 2016-09-27 18:26:24
下面是一个解决方法-
var skills = new Array();
$(document).ready(function() {
$.ajax({
url: "search1.php",
type: "GET",
contentType: contentType,
success: function(data, status, xhr){
skills = data; // ["test", "test2", "test3"]
$( "#skills" ).autocomplete({
source: skills
});
}, error: function(xhr, status, err) {
console.error(err);
}
});
});希望这能有所帮助!
https://stackoverflow.com/questions/39719443
复制相似问题