问题
How do I trigger/call jQuery UI Autocomplete event handlers from each other, for example, triggering a search from the select handler?
Thx, Lille
回答1:
To trigger a search:
$("#my-autocomplete").autocomplete("search", "SearchTerm");
In general, call jQueryUI widget methods using $("selector").widget("method" /*, options */)
回答2:
The answer above is only valid for jQueryUI 1.8.x Since jQueryUI 1.9.x you must add a timeout:
scott.gonzalez says: "There were some pretty big changes to autocomplete between 1.8 and 1.9, specifically selection is now synchronous where before it was asynchronous."
more:
scott.gonzalez says: "What's happening is selecting an item always closes the menu. Closing the menu tells the autocomplete to ignore any pending searches. This includes the search that you're manually triggering immediately before the menu closes. It's also worth noting that you're running a duplicative search, since you're triggering the search before the value updates."
select: function(event, ui)
{
var that = $(this);
setTimeout(function() {
that.autocomplete("search");
}, 1);
},
Example: http://jsfiddle.net/RB4N3/
来源:https://stackoverflow.com/questions/12984151/trigger-search-from-select-within-jquery-ui-autocomplete