2014-09-11 59 views
0

在那里我有一个单词列表我使用这个版本的jQuery的自动完成:jQuery的自动完成匹配无序多个单词

http://jsfiddle.net/K6Dt2/7/

$(function() { 
var availableTags = [ 
    "hello world", "foo bar", "bar foo world", "hello", "bar" 
]; 
function customFilter(array, terms) { 
    arrayOfTerms = terms.split(" "); 
    var term = $.map(arrayOfTerms, function (tm) { 
     return $.ui.autocomplete.escapeRegex(tm); 
    }).join('|'); 
    var matcher = new RegExp("\\b" + term, "i"); 
    return $.grep(array, function (value) { 
     return matcher.test(value.label || value.value || value); 
    }); 
}; 
$("#tags").autocomplete({ 
    source: availableTags, 
    multiple: false, 
    mustMatch: false 
    ,source: function (request, response) { 
     // delegate back to autocomplete, but extract the last term 
     response(customFilter(
     availableTags, request.term)); 
    }, 
}); 
}); 

“世界你好”,“富巴” ,“bar foo world”,“hello”,“bar”

我的问题是,例如在输入“world hello”时,我想只显示“hello world”。

此外,当我键入一个空格,所有选项都显示在我的情况是不正确的。

我该如何实现这种行为?

谢谢

回答

1

不忘强调,你可以尝试反复grepping您的阵列劈裂方面

function customFilter(array, terms) { 
    arrayOfTerms = terms.split(" "); 

    arrayOfTerms.forEach(function (entry) { 
     array = $.grep(array, function (e) { 
      return e.indexOf(entry) >= 0; 
     }) 
    }); 

    return array; 
}