Autocomplete multiple demos: Don't leave the field when tabbing while the menu is open. Fixes #6661 - Autocomplete: Tab on multiple Autocomplete should not change focus after selecting.

This commit is contained in:
Scott González 2010-11-19 16:17:52 -05:00
parent ff4154bb5d
commit 4637695894
2 changed files with 66 additions and 50 deletions

View File

@ -22,35 +22,43 @@
return split( term ).pop(); return split( term ).pop();
} }
$( "#birds" ).autocomplete({ $( "#birds" )
source: function( request, response ) { // don't navigate away from the field on tab when selecting an item
$.getJSON( "search.php", { .bind( "keydown", function( event ) {
term: extractLast( request.term ) if ( event.keyCode === $.ui.keyCode.TAB &&
}, response ); $( this ).data( "autocomplete" ).menu.active ) {
}, event.preventDefault();
search: function() { }
// custom minLength })
var term = extractLast( this.value ); .autocomplete({
if ( term.length < 2 ) { source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
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; return false;
} }
}, });
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> </script>
</head> </head>

View File

@ -43,29 +43,37 @@
return split( term ).pop(); return split( term ).pop();
} }
$( "#tags" ).autocomplete({ $( "#tags" )
minLength: 0, // don't navigate away from the field on tab when selecting an item
source: function( request, response ) { .bind( "keydown", function( event ) {
// delegate back to autocomplete, but extract the last term if ( event.keyCode === $.ui.keyCode.TAB &&
response( $.ui.autocomplete.filter( $( this ).data( "autocomplete" ).menu.active ) {
availableTags, extractLast( request.term ) ) ); event.preventDefault();
}, }
focus: function() { })
// prevent value inserted on focus .autocomplete({
return false; minLength: 0,
}, source: function( request, response ) {
select: function( event, ui ) { // delegate back to autocomplete, but extract the last term
var terms = split( this.value ); response( $.ui.autocomplete.filter(
// remove the current input availableTags, extractLast( request.term ) ) );
terms.pop(); },
// add the selected item focus: function() {
terms.push( ui.item.value ); // prevent value inserted on focus
// add placeholder to get the comma-and-space at the end return false;
terms.push( "" ); },
this.value = terms.join( ", " ); select: function( event, ui ) {
return false; 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> </script>
</head> </head>