Loads the id in Hidden using autocomplete

Asked

Viewed 80 times

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

  • You want to put the id of the selected item or all?

  • When I select the item, it loads the ID of the selected item in Hidden

  • That one $("id_cliente") would be what? Besides this selector being wrong (lack . or #) He’s never run 'cause he’s got a return before. You can show the HTML you have and explain better what you want to happen?

  • I need at the time I select the client name in my textbox, it automatically plays the client ID in a Hidden I used $("#id_client"). val(data.id); but I can’t put the client ID in Hidden

1 answer

1


You must use the event select for this. See 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 on Jsfiddle.

Browser other questions tagged

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