參考這個問題Sorting Autocomplete UI Results based on match location,有一個解決方案,提供了單值jQuery自動完成,但它有可能獲得多個值類似的解決方案jQuery自動完成(http://jqueryui.com/autocomplete/#multiple)?jQuery-ui自動完成多個值按字母順序排序結果
回答
這裏唯一的區別是,你需要確認並請撥打extractLast
,就像您鏈接的演示所做的一樣。下面是完整的代碼應與多個值工作(特別注意的source
選項):
$("#tags")
.on("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function (request, response) {
var term = $.ui.autocomplete.escapeRegex(extractLast(request.term))
// Create two regular expressions, one to find suggestions starting with the user's input:
, startsWithMatcher = new RegExp("^" + term, "i")
, startsWith = $.grep(source, function(value) {
return startsWithMatcher.test(value.label || value.value || value);
})
// ... And another to find suggestions that just contain the user's input:
, containsMatcher = new RegExp(term, "i")
, contains = $.grep(source, function (value) {
return $.inArray(value, startsWith) < 0 &&
containsMatcher.test(value.label || value.value || value);
});
// Supply the widget with an array containing the suggestions that start with the user's input,
// followed by those that just contain the user's input.
response(startsWith.concat(contains));
},
focus: function() {
return false;
},
select: function (event, ui) {
var terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push("");
this.value = terms.join(", ");
return false;
}
});
在響應中,你應該返回符合您在查詢想要什麼結果的列表:
例如
list_of_terms = {"term0","term1","term2",...};
$("#inputsearch").autocomplete({
var term = request.term
var list = new Array();
source: function(request, response) {
var cnt = 0;
$.each(list_of_terms, function(i) {
var rSearchTerm = new RegExp('^' + RegExp.quote(term),'i');
if (list_of_terms[i].match(rSearchTerm)) {
list[cnt] = list_of_terms[i];
cnt++;
}
});
response(list);
}
});
RegExp.quote = function(str) {
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};
提供我沒有錯過一個括號,這應該給你多個值在下拉列表中,如果輸入的一詞中的equals您list_of_terms陣列學期開始
哪裏去這樣?看起來像這是代碼框 - RegExp.quote = function(str){return(str +'')。replace(/([。?* +^$ [] \(){} | - ])/ g, 「\ $ 1」); }; – 2013-02-09 14:04:54
這是javascript。它進入JavaScript腳本標記。你將它鏈接到輸入框的輸入框ID – Tucker 2013-02-09 15:10:40
此代碼在哪裏?'RegExp.quote = function(str){return(str +'')。replace(/([。?* +^$ [] \( ){} | - ])/ g,「\ $ 1」); };'去?因爲它在答案的灰色框外。 – 2013-02-09 15:55:37
- 1. 按字母順序排序結果
- 2. 按字母順序排序jquery結果
- 3. 自動完成擴展器不按字母順序排序
- 4. 按字母順序排序,然後按字母順序排列
- 5. jQuery自動完成結果順序
- 6. 按字母順序排序
- 7. 按字母順序排序
- 8. 按字母順序排序
- 9. 按字母順序排序
- 10. 排序按字母順序
- 11. 按字母順序排序
- 12. 按字母順序排序
- 13. 按字母順序排序的HTML SQL代碼自動排序
- 14. 按類型= text_en按字母順序排序結果
- 15. 按字母順序排序iPhone TableView從RSS按字母順序排序
- 16. 排序按值多維數組按照字母順序
- 17. jQuery用戶界面自動完成按字母順序
- 18. 相交按字母順序排序結果c#
- 19. 按字母順序排序搜索結果
- 20. Wordpress按字母順序排序自定義字段值
- 21. 按字母順序排列
- 22. 按字母順序排列
- 23. 按字母順序排列
- 24. 按字母順序排列PHP排序
- 25. 按兩個順序排序SQL結果
- 26. 按字母順序排序按鈕
- 27. 按字母順序排序按鈕
- 28. 按字段順序排列+按字母順序排列
- 29. jQuery UI自動完成的結果不按順序返回
- 30. 按字母順序排序字符串
謝謝!終於找到了我正在尋找的東西。整潔的解決方案 – iDev 2017-04-13 23:58:50