JQuery UI Autocomplete缓慢选择一个元素

时间:2016-12-25 12:45:24

标签: javascript jquery performance jquery-ui jquery-ui-autocomplete

<script type="text/javascript">
$(function() {
$( "#customers" ).autocomplete({
source: 'search.php'
});
});
</script>
<div class="ui-widget"><input id="customers" name="Cno" placeholder="Customer Name"></div>

的search.php

<?php include('header.php');
//get search term
$searchTerm = $_GET['term'];
//get matched data from skills table
$query = $db->query("SELECT * FROM customers WHERE Customer_Name LIKE '%".$searchTerm."%' ORDER BY Customer_Name ASC");
while ($row = $query->fetch_assoc()) {
$data[] = $row['Customer_Name'];
}
//return json data
echo json_encode($data);
?>

对于某些数组,它在我Jquery UI not working properly for some words中所描述的并不正常。我添加了这段代码

$mysqli->set_charset('utf8mb4') 

然后我在从下拉列表中选择元素时遇到问题,将li类转换为ui-state-active需要太长时间,如何解决?

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

关于你的PHP,我建议:

<强> PHP

<?php
  include('header.php');
  //get search term
  $searchTerm = $_GET['term'];
  $data = array();
  //get matched data from skills table
  $query = $db->prepare("SELECT * FROM customers WHERE Customer_Name LIKE '%?%' ORDER BY Customer_Name ASC");
  $query->bind_param('s', $searchTerm);
  $query->execute();
  $results = $query->get_result();
  while ($row = $results->fetch_assoc()) {
    $data[] = $row['Customer_Name'];
  }
  $query->close();
  $db->close();
  //return json data
  header('Content-Type: application/json');
  echo json_encode($data);
?>

这将有助于保护脚本免受SQL注入。

关于你的jQuery,我建议:

<强>的JavaScript

$(function() {
  $("#customers").autocomplete({
    minLength: 3,
    source: 'search.php'
  });
});

如果您输入&#39; sim&#39;,自动完成功能会通过GET将此内容发送给您的PHP。响应将类似于:

[
  "Bart Simpson",
  "Homer Simpson",
  "Lisa Simpson",
  "Maggie Simpson",
  "Marge Simpson"
]

在您的控制台中,您应该看到此活动,并可以查看执行时间。这将告诉您PHP花了多长时间来提供对请求的响应。此数据应立即加载到自动完成中。

如果您看到缓慢,则必须确定它是否在您的PHP或JavaScript中。这将决定在哪里寻找问题。