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

查询:

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

//这是从数据库中检索数组并输出它,这是执行此操作的代码:
    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;
}

HTML:
<input id="keywords" name="keywords" type="text" autocomplete="off" size="40" >

最佳答案

在你的 css 中添加:

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

img/indicator.gif 替换为您的图像预加载路径,如果您希望预加载位于左侧,也可以通过 right 更改 left
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);
        }

关于php - 将预加载器添加到 jquery 自动完成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10418094/

10-12 14:04