Get autocomplete return and put on different inputs

6

I have the following script

<script type="text/javascript">
     $().ready(function() {
          $("#course").autocomplete("teste1.php", {
              width: 260,
              matchContains: true,
              selectFirst: false
      });
    });
</script>

What gets the return from here:

$q = strtolower($_GET["q"]);
if (!$q) return;

list($resultados,$quantidade) = $select->selectTable('f_tb_empresas','id_empresa,nm_empresa',NULL,"nm_empresa LIKE '%".$q."%'",NULL); 

foreach ($resultados as $row) {
    $linha['value'] = $row['nm_empresa'];
    $linha['id'] = $row['id_empresa'];

}

echo json_encode($linha);

And the result comes to input :

<input type="text" name="course" id="course" />

The result looks like this: {"value":"Alameda dos anjos","id":"39"} , but I wanted the id to be one input and value to another.

How do I do this with JQuery?

Thank you

    
asked by anonymous 03.12.2015 / 17:44

1 answer

1

I recommend using jQuery UI because this jquery.autocomplete.js file is an outdated version of the function.

You can achieve the separate values by following this example:

$("#course").autocomplete("teste1.php", {
    width: 260,
    matchContains: true,
    select: function( event, ui ) {
       $( "#campoNome" ).val( ui.item.label );
       $( "#campoValor" ).val( ui.item.value );    
    }
});

Take a look at the documentation: link

    
08.12.2015 / 03:22