3

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

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

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

有人有线索吗?

4

2 回答 2

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 } );
    }
});
于 2014-11-08T11:35:23.677 回答
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 );
    }
});
于 2018-06-27T07:46:04.713 回答