1
Good afternoon, everyone.
I am trying to implement an autocomplete using typeahead.js. Assigns a change event to my input to make an ajax request that brings me the data that will be displayed in the list.
However, when the screen is loaded and I type something my event is not triggered. If I take the focus of the component and type again the event is also not fired. It is triggered only when I click on the address bar, or switch tab in the browser or window. After the event is triggered for the first time, every time I type something the event is triggered normally. Any idea why this might be happening?
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
$(function(){
$("#nome").change(function(){
var input = $(this).val()
$.ajax({
url: "loadNameChoices",
type: "get",
data:{input:input},
contentType: 'application/json',
success: function(response) {
$("#nome").typeahead({source:response});
},
});
});
});
</script>
</head>
<body>
<div class="container-fluid">
<div class="panel panel-primary">
<g:form name="formBusca" class="form-inline">
<div class="panel-body">
<div class="form-group">
<label>Nome:</label>
<g:textField id="nome" name="nome" class="form-control" autocomplete="off"/>
</div>
<div class="form-group">
<label>Usuário:</label>
<g:textField id="username" name="username" class="form-control" autocomplete="off"/>
</div>
</div>
<div class="panel-footer">
<div class="text-right">
<button type="button" class="btn btn-default btn-sm" id="search-button">Pesquisar</button>
<g:link action="form" class="btn btn-default btn-sm">Novo</g:link>
</div>
</div>
</g:form>
</div>
</div>
</body>
</html>
The Backend of the application is being ripped with Grails, but I do not believe that this has any relation.
Thank you.
Great answer. Thank you!!
– user8078