duansha3771 2013-03-03 15:51
浏览 32
已采纳

Ajax auto建议不按顺序显示字母。

I am making a small script with javascript to find/display text from an array as I am typing. Sort of like, Google auto suggest tool.

Here is Js script:

<script>
$(function() {
    var availableTags = <?php echo json_encode( $foo ); ?>;
     $( "#tags" ).autocomplete({ source: availableTags });
});
</script>

and the arrays in PHP

  <?php 
        $foo = array("ambiguious","brown",
        "corps","demanding job","eat the pomes","fooling with it");
    ?>

Notice how each words in the array have different first letters than the rest ie.? good, now when I type a instead of showing words that being with a (in this case "ambiguious"), it shows me all words which has a in them.

enter image description here

I thought about making strpos() to search through similar words, but it does not work. Any thoughts would be nice. Thank you.

  • 写回答

2条回答 默认 最新

  • douqiao3930 2013-03-03 16:00
    关注

    have a look at the API:

    http://api.jqueryui.com/autocomplete/

    Example: Using a custom source callback to match only the beginning of terms

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>autocomplete demo</title>
      <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css">
      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
      <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
    </head>
    <body>
    
    <label for="autocomplete">Select a programming language: </label>
    <input id="autocomplete">
    
    <script>
    var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
    $( "#autocomplete" ).autocomplete({
      source: function( request, response ) {
              var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
              response( $.grep( tags, function( item ){
                  return matcher.test( item );
              }) );
          }
    });
    </script>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?