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条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来