JQuery UI自动完成选择模糊或关闭

时间:2012-11-20 16:09:25

标签: jquery-ui jquery-ui-autocomplete

我想在模糊或关闭时从输入自动完成中选择值。

我试图把它放在close参数的函数中,但它无法工作:

close: function() {
  $($(this).data('autocomplete').menu.active).find('a').trigger('click');
  $(this).removeClass('ui-corner-top').addClass('ui-corner-all');
}

有人有线索吗?

2 个答案:

答案 0 :(得分:0)

好的,感谢Andrew Whitaker,我得到了这个解决方案,在离开自动完成时自动选择第一个值:

$.ui.autocomplete.prototype.options.autoSelect = true;
$( ".ui-autocomplete-input" ).live( "blur", function( event ) {

    var autocomplete = $( this ).data( "autocomplete" );
    if ( !autocomplete.options.autoSelect || autocomplete.selectedItem ) { return; }

    autocomplete.widget().children( ".ui-menu-item:first" ).each(function() {
        var item = $( this ).data( "item.autocomplete" );
        autocomplete.selectedItem = item;
    });
    if ( autocomplete.selectedItem ) {
        autocomplete._trigger( "select", event, { item: autocomplete.selectedItem } );
    }
});

答案 1 :(得分:0)

jQuery UI的更新代码-v1.12.1

$.ui.autocomplete.prototype.options.autoSelect = true;
$( ".ui-autocomplete-input" ).live( "blur", function( event ) {
    var autocomplete = $(this).autocomplete('instance');

    if (!autocomplete.options.autoSelect || autocomplete.selectedItem ) { return; }

    autocomplete.widget().children( ".ui-menu-item:first" ).each(function() {
        var item = $( this ).data('ui-autocomplete-item');
        autocomplete.selectedItem = item;

    });

    if ( autocomplete.selectedItem ) {
        autocomplete._trigger( "select", event, { item: autocomplete.selectedItem } );
        autocomplete._value( autocomplete.selectedItem.value );
    }
});