Loads id in hidden using autocomplete

0

Hello, I have the following script, autocomplete:

<script type="text/javascript">
        $(document).ready(function () {
            $(".nomeCliente").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "/PreVenda/CarregarCliente",
                        type: "POST",
                        dataType: "json",
                        data: { nome_razao: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    label: item.nome_razao,
                                    value: item.nome_razao,
                                    id: item.id
                                };
                                $("id_cliente").val(data.id);
                            }))

                        }
                    })
                },
                messages: {
                    noResults: "", results: ""
                }
            });
        })
    </script>

I would like to know how to put the ID in a hidden

    
asked by anonymous 29.08.2017 / 20:03

1 answer

1

You should use the select event for this. Here's a basic example below:

$(document).ready(function() {
  $(".nomeCliente").autocomplete({
    delay: 100,
    source: function(request, response) {
      var suggestURL = "http://suggestqueries.google.com/complete/search?client=chrome&q=%QUERY";
      suggestURL = suggestURL.replace('%QUERY', request.term);
      $.ajax({
          method: 'GET',
          dataType: 'jsonp',
          jsonpCallback: 'jsonCallback',
          url: suggestURL
        })
        .success(function(data) {
          response(data[1]);
        });
    },
    select: function(event, ui) {
      console.log(ui)
      $("#id_cliente").val(ui.item.value);
    },
    messages: {
      noResults: "sasa",
      results: "sasas"
    }
  });
});

See working in JSFiddle.

    
29.08.2017 / 20:34