问题
i have this input with autocomplete feature:
.autocomplete({
source: "jsonR.php",
minLength: 2,
select: function( event, ui ) {
$(this).val(ui.item.value);
llamar('/includes/router.php?nomenu=1&que=view_item&id='+ui.item.id,'router');
return false;
},
autoFocus: true
,open: function() {
$('.ui-autocomplete').addClass('searchBox');
}
})
Basically i want the feature to select the first item so if user hits enter it searches the first one but this wont hover/focus the first item suggested,
any idea why?
ps: the rest works fine, dough
回答1:
Scott Gonzalez wrote a selectfirst plugin for this.
See the details here: http://forum.jquery.com/topic/autocomplete-automatically-select-first-item-in-dropdown-or-add-item-into-drop-down-menu
You can download the plugin here: https://github.com/scottgonzalez/jquery-ui-extensions/blob/master/autocomplete/jquery.ui.autocomplete.selectFirst.js
better yet, here is the source code of the plug-in :)
/*
* jQuery UI Autocomplete Select First Extension
*
* Copyright 2010, Scott González (http://scottgonzalez.com)
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* http://github.com/scottgonzalez/jquery-ui-extensions
*/
(function( $ ) {
$( ".ui-autocomplete-input" ).live( "autocompleteopen", function() {
var autocomplete = $( this ).data( "autocomplete" ),
menu = autocomplete.menu;
if ( !autocomplete.options.selectFirst ) {
return;
}
menu.activate( $.Event({ type: "mouseenter" }), menu.element.children().first() );
});
}( jQuery ));
NOTE: to use this simply add this line to your autocomplete:
selectFirst: true, // auto selects first element
来源:https://stackoverflow.com/questions/9095856/jqueryui-autocomplete-autofocus-true-wont-do-anything