我在我的网站中使用https://jqueryui.com/autocomplete/#multiple来获取自动填充功能。它的工作正常,现在我想用第一个添加到文本框的字符搜索记录。
这是我的代码:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css"><input type="text" id="multi"><script>$(document).ready(function() {var avail = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
];
$("#single").autocomplete({
source:avail
});
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#multi" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
avail, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});});</script>
答案 0 :(得分:1)
根据评论:
要从第一个字符开始typeahead.js multipel search demo搜索“,请更改匹配的代码行:
~item.toLowerCase().indexOf(tquery.toLowerCase())
到此:
item.toLowerCase().indexOf(tquery.toLowerCase()) == 0
演示小提琴:http://jsfiddle.net/BwDmM/1100/
补充说明:可以在此处找到有关原始匹配代码如何工作的更多解释(tilda按位运算符):http://www.joezimjs.com/javascript/great-mystery-of-the-tilde/