0
When selecting the product in the list of suggestions Autocomplete I have to give TAB to complete the next fields, I wanted that as the Description field was filled in the other fields were filled out without having to use TAB. Would have as ?
<div class="container">
<div class="col-lg-12">
<div class="col-lg-12">
<h3 align="center">Autocomplete utilizando jQuery, PHP e MySQL</h3><br />
<label>Descrição</label>
<input type="text" name="descri" id="descri" class="form-control" placeholder="Escreva aqui a descrição do produto"><br>
<div id="descriList"></div>
</div>
<div class="col-lg-4"><!-- Inicio Input Cóodigo do Produto-->
<label for="ex1">Código do Produto:</label>
<input type="text" required class="form-control" maxlength="13" name="codigo_produto"><br>
</div><!-- Fim Input Código do Produto -->
<div class="col-lg-4"><!-- Inicio Input Código EAN / Barcode -->
<label for="ex1">Código EAN:</label>
<input type="text" required class="form-control" maxlength="13" name="barcode"><br>
</div><!-- Fim Input Código EAN / Barcode -->
<div class="col-lg-4"><!-- Inicio Input ID -->
<label for="ex1">ID:</label>
<input type="text" disabled class="form-control" maxlength="13" name="id"><br>
</div><!-- Fim Input ID -->
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#descri').keyup(function(){
var query = $(this).val();
if(query != '')
{
$.ajax({
url:"search.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#descriList').fadeIn();
$('#descriList').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#descri').val($(this).text());
$('#descriList').fadeOut().change();
});
});
$("input[name='descri']").on("change",function(){
alert("Campo alterado!");
var $codigo_produto = $("input[name='codigo_produto']");
var $barcode = $("input[name='barcode']");
var $id = $("input[name='id']");
var $unid = $("select[name='unid']");
var $familia = $("select[name='familia']");
var $tipo = $("select[name='tipo']");
var $id_depto = $("select[name='id_depto']");
var $grupo = $("select[name='grupo']");
var $cod_referencial = $("input[name='cod_referencial']");
var $codigo_interno = $("input[name='codigo_interno']");
$.getJSON('function.php',{
descricao: $( this ).val()
},function( json ){
$codigo_produto.val( json.codigo_produto );
$barcode.val( json.barcode );
$id.val( json.id );
$unid.val( json.unid );
$familia.val( json.familia );
$tipo.val ( json.tipo );
$id_depto.val( json.id_depto );
$grupo.val( json.grupo );
$cod_referencial.val( json.cod_referencial );
$codigo_interno.val( json.codigo_interno );
});
});
</script>
If you want the function to be executed each time something is typed in the field, just use
$("input[name='descri']").on("input",function(){})
.– Sam
wanted the function to be executed by clicking on something suggested by the autocomplete list and filling in the name input field
– Victor
Puts
$("input[name='descri']").on("change",function(){
– Sam
tried and it doesn’t work, should I change the second script
– Victor
Here it worked, see a test: http://decknorte.com/teste.php
– Sam
type anything in the "description" field and then click on any <li> that will appear
– Sam
in case the autocomplete works, the search related data appears in the 'description' field but when selecting something / click on something, instead of completing the fields as soon as the description field is filled, How to give TAB for the execution of the second function and then complete the next fields
– Victor
No no. See Alert, you have already entered the function that will complete the other fields.
– Sam
You need to give TAB no. By clicking on the <li> it fills the "description" field and calls the function to the other fields.
– Sam
I changed the code there to fill in the fields. Note that it works perfectly by clicking on a <li>: http://decknorte.com/teste.php
– Sam
in the case in my code it does not work, only recognizes what was typed in the input field, I edited the question to show how this
– Victor
Let’s go continue this discussion in chat.
– Sam