JqueryUI Autocomplete: autoFocus = true won't do anything

白昼怎懂夜的黑 提交于 2019-12-10 17:35:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!