jim
jim

Reputation: 43

jquery autocomplete how to get the id

I'm attempting to use JQuery autocomplete and need to get the id of the value that was populated in the box. I'm using a hidden element to to store the id value in my html. The autocomplete works just fine and shows the results until I add the id to the array, then stops showing results.

This is an example of what my server code is returning.

{"id":11,"value":"aaaa"}

This is the JQuery

  $("#user").autocomplete({
    source: "/item/user.php",
    minLength: 2,
    select: function(event, ui) {
      $('#userId').val(ui.item.id);
    }
  });

Upvotes: 4

Views: 1741

Answers (1)

Andrew Orsich
Andrew Orsich

Reputation: 53695

You suggest you need to add something like this:

 $("#user").autocomplete({
    source: "/item/user.php",
    minLength: 2,
    dataType: "json",
    select: function(event, ui) {

      $('#userId').val(ui.value);
    },
    parse: function (data) {
                return $.map(arr, function (row, i) {
                    return {
                        data: row.id,
                        value: row.value,
                        result: row.value
                    }
                });
            },
    });

So, value in select option will be userId, and text - userName.

Set breakpoint at select function and check value of 'ui' at firebug.

Upvotes: 5

Related Questions