jQuery-ui autocomplete multiple values sort results alphabetically
参考这个问题 Sorting Autocomplete UI Results based on match location ,有一个提供单值 jQuery 自动完成的解决方案,但是否有可能为多值 jQuery 自动完成获得类似的解决方案(http://jqueryui.com /自动完成/#multiple)?
这里唯一的区别是你需要确保和调用 extractLast 就像你链接到的演示正在做的那样。这是应该使用多个值的完整代码(特别注意 source 选项):
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
$(“#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, |
示例:http://jsfiddle.net/Aa5nK/1/
- 谢谢!终于找到了我正在寻找的东西..整洁的解决方案
在响应中,您应该返回与您的查询匹配的结果列表:
例如
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
list_of_terms = {“term0″,”term1″,”term2”,…};
$(“#inputsearch”).autocomplete({ RegExp.quote = function(str) { |
如果我没有遗漏括号,如果输入的术语等于 list_of_terms 数组中术语的开头,这应该会在下拉列表中为您提供多个值
- 这要去哪里?看起来这是在代码框之外 – RegExp.quote = function(str) { return (str \\’\\’).replace(/([.?* ^$[](){}|-])/g, “\\\\\\\\$1”); };
- 这是javascript。它包含在 javascript 的脚本标签中。您通过输入框ID将其链接到您的输入框
- 这段代码 – RegExp.quote = function(str) { return (str+”).replace(/([.?*+^$[]\\(){}|-])/g,”\\$1″); }; 去哪里了?因为它在答案中的灰色框之外。
- 这也进入了javascript。它应该是灰色的。我会解决的
来源:https://www.codenong.com/14784794/