如何使自动完成只开始应该在文本框中给出字母?

时间:2014-09-10 08:51:21

标签: jquery

http://jsfiddle.net/9R4cV/116/

在这个小提琴自动完成中你喜欢的是如果我输入字母“a”它的列表,即使“a”在单词的中间,但我只需要开始单词“a”请一些人给我解决方案。

 $(document).ready(function() {

  var aTags = ["ask","always", "all", "alright", "one", "foo", "blackberry", "tweet","force9",   "westerners", "sport"];
  $( "#tags" ).autocomplete({
    source: aTags
  });
 });

2 个答案:

答案 0 :(得分:2)

我更新了你的小提琴http://jsfiddle.net/9R4cV/119/

有关详细信息,请参阅此答案:https://stackoverflow.com/a/2405109/3810453

以下是实施:

    $(document).ready(function() {

    var aTags = ["ask","always", "all", "alright", "one", "foo", "blackberry", "tweet","force9", "westerners", "sport"];

    $( "#tags" ).autocomplete({
        //source option can be an array of terms.  In this case, if
    // the typed characters appear in any position in a term, then the
    // term is included in the autocomplete list.
    // The source option can also be a function that performs the search,
    // and calls a response function with the matched entries.
    source: function(req, responseFn) {
        var re = $.ui.autocomplete.escapeRegex(req.term);
        var matcher = new RegExp( "^" + re, "i" );
        var a = $.grep( aTags, function(item,index){
            return matcher.test(item);
        });
        responseFn( a );
    }
});
});

答案 1 :(得分:0)

documentation,您可以使用自定义来源:

var aTags = ["ask","always", "all", "alright", "one", "foo", "blackberry", "tweet","force9",   "westerners", "sport"];
$("#tags").autocomplete({
   source: function( request, response ) {
     var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
     response( $.grep( aTags, function( item ){
       return matcher.test( item );
     }) );
   }
});