Reputation: 10561
I have a jQuery autocomplete, and the select
event is never fired.
The script looks like this:
$(function() {
$('#AppName').autocomplete({
minLength: 0,
source: function(request, response) {
var url = "/api/AddAppAjax";
$.getJSON(url, { term: request.term }, function(data) {
response(data);
})
},
select: function (event, ui) {
alert(ui.item.id); <---- !This is never hit!
$('#selected-id').val(ui.item.id);
}
});
})
The Web API controller action looks like this:
public IEnumerable<object> Get(string term)
{
var appManager = new AppManager();
var appList = appManager.GetAllApps().AsQueryable().ToList();
var appListJson = from fbApp in appList
select new
{
id = App.Id,
value = App.AppName,
label = App.ToString()
};
return appListJson;
}
The autocomplete itself works fine. It's just the event that isn't firing. I tried binding to the event with bind
and on
and that didn't help either. I also tried this on the change
event, and that does not get fired either, or so it looks to me. I can't figure out why the event is not firing.
Upvotes: 2
Views: 6102
Reputation: 11
I had the same problem. The problem for me was that a secondary autocomplete targeted the same element.
In your case it could be that ('something').autocomplete() also hits the #AppName element.
Upvotes: 0
Reputation: 1360
I think so you need to add this too...
$( "#AppName" ).on( "autocompleteselect", function( event, ui ) {} );
Upvotes: 3
Reputation: 2585
It looks like, you have to return response from source function
$(function() {
$('#AppName').autocomplete({
minLength: 0,
source: function(request, response) {
var url = "/api/AddAppAjax";
$.getJSON(url, { term: request.term }, function(data) {
return response(data);
})
},
select: function (event, ui) {
alert(ui.item.id); <---- !This is never hit!
$('#selected-id').val(ui.item.id);
}
});
})
Upvotes: -2