针对特定情况替代 .change()

Alternative to .change() for specific situation

我想达到的目标:

我有一个使用 jquery ui 自动完成功能的文本框,我想要实现的是当一个选项被点击并从自动完成下拉列表中选择时我想要一个事件触发。

问题

我遇到的问题是,我尝试使用 .change() ,但它只在您单击该元素时触发。 (这个问题之前在这个postWhy does jQuery.change() only trigger when I leave the element?中已经问过)。

但是在这种情况下,该线程中的解决方案对我没有用。因为我希望在选择下拉列表中的元素并将其加载到文本框中时触发事件。我不想点击文本框以外的地方来触发 .change() 事件。

理想的结果:

当用户从自动完成列表中选择一个选项时,它会触发事件,而无需用户单击文本框。

我想要发生的相关事件是在用户选择一个选项后隐藏自动完成文本框:

$(search_box).change( function () {
    $(search_box).hide();
} );

这有效,但正如我上面提到的,用户必须单击离开才能触发更改事件。有什么解决办法吗?

您可以使用 autocompleteselect 事件处理程序作为 $( ".selector" ).on( "autocompleteselect", function( event, ui ) {} ); 这是

Triggered when an item is selected from the menu. The default action is to replace the text field's value with the value of the selected item.

here