I'm using multiple inputs in a bootstrap modal window (which is loaded via AJAX). I want on those inputs work typeahead.js integrated with Bloodhound. The datasets are loaded correctly (as JSON), but for some reason I do not understand the inputs are automatically disabled. I'm using the latest versions of everything.
Here the inputs tag (with the related hidden input). The engine takes data-autocomplete to finish building the remote URL.
<input type="text" class="typeahead form-control" data-provide="typeahead" id="my_input" data-autocomplete='users'>
<input type="hidden" name="my_input" />
Here my JavaScript code:
$.get(window.BASE_URL+'/form',function(data){
/*SUGGESTIONS ENGINE*/
var autocomplete;
var autocomplete = new Bloodhound({
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.id); },
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 20,
remote: {
url: window.BASE_URL+'the-url/',
replace: function(url,uriEncodedQuery) {
if(typeof $('input:focus').attr('data-autocomplete')!='undefined')
{
var new_url=url+$('input:focus').attr('data-autocomplete')+'?texto='+$('input:focus').val()+'&tipo='+$('input:focus').attr('id');
return new_url;
}
}
}
});
autocomplete.initialize();
//LOAD TYPEAHEADS
$('.modal-body').find('input.typeahead').typeahead({
minLength: 2,
highlight:true
},{
name: 'autocomplete',
displayKey:'label',
source: autocomplete.ttAdapter(),
}).on('typeahead:selected', function(obj, datum) {
//console.log(JSON.stringify(this));
//If the text has an id seeks its hidden twin
if(typeof $(this).attr('id')!='undefined')
$('input[name="'+$(this).attr('id')+'"]').val(datum.id);
});
});
The format of an item from the json response is as follows:
{"id":"1","label":"McDonald, Richard"}