我正在实现jquery自动完成,这是代码,
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Scheme"
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, 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>
以下是HTML代码:
<label for="tags">Tag programming languages: </label>
<textarea rows="3" cols="50" id="tags"></textarea>
现在问题是,当我输入“a”或“s”时,我从自动完成中获取相关数据。我想以这样的方式实现,当我按下“空格键”时,我需要获取所有数据。我该怎么做呢请帮忙......
答案 0 :(得分:0)
如果您可以定义一个函数extractAll,您可以按照以下方式执行
if(request.term == " ")
response( $.ui.autocomplete.filter(
availableTags, extractAll() ) );
else
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );