]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete multiple demos: Don't leave the field when tabbing while the menu is...
authorScott González <scott.gonzalez@gmail.com>
Fri, 19 Nov 2010 21:17:52 +0000 (16:17 -0500)
committerScott González <scott.gonzalez@gmail.com>
Fri, 19 Nov 2010 21:17:52 +0000 (16:17 -0500)
demos/autocomplete/multiple-remote.html
demos/autocomplete/multiple.html

index cfbf56fe1cc3391d7a89eea2b929c42253aafd8f..9494da98abe40ad45980714a9c0fe68249b709f3 100644 (file)
                        return split( term ).pop();
                }
 
-               $( "#birds" ).autocomplete({
-                       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 ) {
+               $( "#birds" )
+                       // 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({
+                               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;
                                }
-                       },
-                       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>
 </head>
index 33f0ad8343a13666b275ddccf9235a8cdf111e74..44e6971e507f7f7b43ebb978bcadcd9cbbc8814d 100644 (file)
                        return split( term ).pop();
                }
 
-               $( "#tags" ).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;
-                       }
-               });
+               $( "#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>
 </head>