向jQuery自动完成添加预加载器

luv*_*ode 3 php jquery autocomplete preloader

我正在使用Jquery的自动完成功能从mysql数据库中检索选项列表,并在搜索时输出它们。但是有时需要一分钟,因此我想在输入搜索查询时添加一个小的预加载的gif。我已经在Google和此处搜索过,还没有找到我需要的答案。如果有人可以帮助,将不胜感激!这是我的代码:

JQUERY:

<script>
$(document).ready(function() {
  $("#keywords").autocomplete({
    source: keywordList,
    minLength: 2,
    select: function(event, ui){
        $("#keywords").val(ui.item.value);
        } 
  });
});
</script>
<?php echo keywordArray(); ?> 
Run Code Online (Sandbox Code Playgroud)

//这是从数据库中检索数组并将其输出,这是执行此操作的代码:

    function keywordArray()
{
  $rsKeywords = mysql_query("SELECT Destination FROM Destinations WHERE Country = 'Mexico'");

  $output = '<script>'."\n";
  $output .= 'var keywordList = [';

  while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
  {
    $output .= '"'.$row_rsKeywords['Destination'].'",';
  }

  $output = substr($output,0,-1); //Get rid of the trailing comma
  $output .= '];'."\n";
  $output .= '</script>';
  return $output;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<input id="keywords" name="keywords" type="text" autocomplete="off" size="40" >
Run Code Online (Sandbox Code Playgroud)

mgr*_*aph 5

在您的CSS中添加以下内容:

.ui-autocomplete-loading { background:url('img/indicator.gif') no-repeat right center }
Run Code Online (Sandbox Code Playgroud)

更换img/indicator.gif你的图像预紧路径,你也可以改变rightleft,如果你想预载是在左侧

js:

$("#keywords").autocomplete({
    source: keywordList,
    minLength: 2,

    search  : function(){$(this).addClass('ui-autocomplete-loading');},
    open    : function(){$(this).removeClass('ui-autocomplete-loading');},

    select: function(event, ui){
        $("#keywords").val(ui.item.value);
        } 
Run Code Online (Sandbox Code Playgroud)