jQuery自动完成(远程) – 示例

我真的希望避免发布一个新问题,但我找不到一个包含调用页面和“搜索”页面的jQuery自动完成远程function的function示例。 jQueryUI“演示和文档”部分不包含“search.php”的来源

我已经尝试了几十种组合,但这是我开始的:

 .ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }   $(function() { function log( message ) { $( "
" ).text( message ).prependTo( "#log" ); $( "#log" ).attr( "scrollTop", 0 ); } $( "#birds" ).autocomplete({ source: "search.php", minLength: 1, select: function( event, ui ) { log( ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.id : "Nothing selected, input was " + this.value ); } }); });
Result:

和search.php:

  $conn = mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE", $conn); $q = strtolower($_GET["birds"]); $query = mysql_query("select FIELD from TABLE where FIELD like '%$q%'"); while ($row = mysql_fetch_array($query)) { echo json_encode($row); } 

有没有人有代码片段显示他们可以分享的这个等式的两边? 非常感谢您提供的任何帮助。

这是search.php的正确代码:

  $conn = mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE", $conn); $q = strtolower($_GET["term"]); $return = array(); $query = mysql_query("select FIELD from TABLE where FIELD like '%$q%'"); while ($row = mysql_fetch_array($query)) { array_push($return,array('label'=>$row['FIELD'],'value'=>$row['FIELD'])); } echo(json_encode($return)); 

一些关键点

  • 单词term在jqueryui给出的样本调用页面中没有任何地方,但这是使用的Querystring名称
  • 您必须创建值的数组,然后在返回之前进行json编码

我希望这有助于将来的一些人!

当你谷歌搜索’jQuery UI Autocomplete Tutorial’时,有几个较大的教程,我想这些可能有任何帮助: http : //www.google.com/search? q = jqueryUI + autocomplete +tutorial

这是我以前使用的自动完成的修剪版本。 因为我删除了一些代码,可能会有一两个错误。

在服务器上:

  if(isset($_POST['queryString'])) { $queryString = $_POST['queryString']; $html = ''; if(strlen($queryString) >1) { $names= explode(" ", $queryString ); foreach ($names as &$value) { // step 1: first names $result= queryf("SELECT *, MATCH(productName, productTags, productCategory, productOneLine) AGAINST ('*$queryString*' IN BOOLEAN MODE) AS score FROM products WHERE MATCH(productName, productTags, productCategory, productOneLine) AGAINST ('*$queryString*' IN BOOLEAN MODE) AND productStatus='1' ORDER BY score DESC LIMIT 10") ; if($result) { while ($row = mysql_fetch_array($result)) { echo '
  • '.$row['productName'].'
    '.$row['productOneLine'].'
  • '; } } } else { echo '
    • Processing...
      Please keep typing while we process your code
    '; } } } else { echo '
    • Processing...
      Please keep typing while we process your code
    '; } } else { echo 'Nothing to see here.'; }

    剧本:

     function suggest(inputString){ if(inputString.length == 0) { $('#suggestions').fadeOut(); } else { $('#country').addClass('load'); $.post("/autosuggest.php", {queryString: ""+inputString+""}, function(data){ if(data.length >0) { $('#suggestions').fadeIn(); $('#suggestionsList').html(data); $('#country').removeClass('load'); } }); } } function fill(thisValue) { $('#country').val(thisValue); setTimeout("$('#suggestions').fadeOut();", 600); } 

    在XHTML页面上:

     

    关于“接受率”的评论是不必要的,发布谷歌比声誉更有用。

    我做了像喜欢和它成功,我使用的是mysqli方法。

      $q = strtolower($_GET["term"]); $return = array(); $query = "select name from students where name like '%$q%'"; $result=$conn->query($query); while ($cresult=$result->fetch_row()){array_push($return,array('label'=>$cresult[0],'value'=>$cresult[0])); } echo(json_encode($return)); 

    ?>