Take autocomplete return and put in different inputs

Asked

Viewed 552 times

6

I have the following script

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

That takes the turn 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 the input:

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

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

How do I do this with Jquery?

Thank you

  • the value of $q is being passed directly to the untreated query before? >.<

  • @Renatotavares for now yes

  • Where is the JS that returns the JSON ?

  • @Taopaipai This is my question, how do I return this value? because the way it is in the code the source goes directly to input

  • This script that has the function autocomplete, has the link ?

  • <script type="text/javascript" src="js/jquery-1.4.2.js"></script>&#xA;<script type='text/javascript' src="js/jquery.autocomplete.js"></script><link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" /> @Taopaipai

  • Yes, but you have the LINK of the documentation of this jquery.autocomplete.js ? If you don’t, fine. Just explain to me one thing... you said you want the value in a input and the id in another input. At what point do you want this ? When do you return the data or when do you choose an ? If it’s the first option it doesn’t have to be the two in the same input ?

  • @Taopaipai when I choose an option, I would like the one input to receive the value and the other the id

  • I get it, for that you have to see the elements it generates under the input. Open the Chrome Inspect Element and type something into the field to run the autocomplete. In the result that appears right click on a result and go to Inspect Element. Put a print on your post of what appears.

  • @Taopaipai image link: (https://drive.google.com/file/d/0BwH8KrvYWlojV3JSbkN3d2pHbU0/view?usp=sharing)

Show 5 more comments

1 answer

1

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

You can get 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: https://jqueryui.com/autocomplete/#custom-data

Browser other questions tagged

You are not signed in. Login or sign up in order to post.