JqueryUI自动完成:autoFocus = true将不会执行任何操作

我有自动完成function的输入:

.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'); } }) 

基本上我希望该function选择第一项,所以如果用户点击进入它搜索第一项但这不会hover/聚焦建议的第一项,

知道为什么吗?

ps:其余的都很好,面团

Scott Gonzalez为此编写了一个selectfirst插件。

详情请见: http : //forum.jquery.com/topic/autocomplete-automatically-select-first-item-in-dropdown-or-add-item-into-drop-down-menu

你可以在这里下载插件: https : //github.com/scottgonzalez/jquery-ui-extensions/blob/master/autocomplete/jquery.ui.autocomplete.selectFirst.js

更好的是,这里是插件的源代码:)

 /* * 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 )); 

注意:要使用此function,只需将此行添加到自动完成function:

 selectFirst: true, // auto selects first element