Autocomplete insert the value into two different inputs?

0

I have this code

        <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
        <script type='text/javascript' src="js/jquery.autocomplete.js"></script>
        <link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
        <script type="text/javascript">



            $().ready(function() {
               

                $("#course").autocomplete("autocomplete.php", {
                    width: 300,
                    matchContains: true,
                    mustMatch: true,
                    minChars: 0,
                    //multiple: true,
                    highlight: false,
                    //multipleSeparator: ",",
                    selectFirst: true
                });
            });



        </script>

<input type="text" name="course" id="course" size="100"/><br><br><br>
<input type="text" name="id" id="id" size="100">

That generates this result:

AndIhavetwoinputtextinit.Itispossibletonoticethatwhentheuserpressesakey,thefunctioncallsthefileautocomplete.phpwhichreturnsaresultinjson.

  

{"name": "Augustine", "id": "1"}

What I need now:

I wanted to distribute the results: nome and id on different inputs. The name go to the course field and the id go to the id field

How can I proceed in this case?

    
asked by anonymous 01.08.2017 / 21:59

1 answer

0

Use JSON.parse(json) to transform the autocomplite result into an object.

This way you can do this:

var pessoa = JSON.parse(json);
$('#input-nome').val(pessoa.nome);
$('#input-id').val(pessoa.id);
    
02.08.2017 / 17:28