0

I have the following code shown below. When I uncomment the area that has

    alert('Key ' + item.KeyID)

I do get the key value just fine. But when I do:

     select: function (event, ui)                        

and try to fetch the KeyID it shows as undefined.

$(document).ready(function () {
   $('#Company').autocomplete(
           {
               source: function (request, response) {
                   $.ajax({
                       url: '@Url.Action("AutoCompleteCompany", "Main")',
                       type: "POST",
                       dataType: "json",
                       data: { term: request.term },
                       success: function (data) {
                           response($.map(data, function (item) {
                               //alert('Key ' + item.KeyID);
                               //alert('Value ' + item.KeyValue);
                               return {label: item.KeyValue, val: item.KeyID };
                           }))
                       }
                   })
               },
               select: function (event, ui) {                       

                  alert(ui.item.KeyID);   // shows undefined                    


               }
           });
})

Nate Pet
  • 44,246
  • 124
  • 269
  • 414

1 Answers1

0

Try the following load in an array the source and then load the autocomplete object

function loadData() {
  $.ajax({
    url: '@Url.Action("AutoCompleteCompany", "Main")',
    type: "POST",
    dataType: "json",
    data: { term: request.term },
    success: function (data) {
      jsdata = data.d;
      for (k = 0; k < jsdata.Object.length; k++) {
        arrayObject.push({ label: jsdata.Object[k].nombre, id: jsdata.Object[k].id, other: jsdata.Object[k].other });
      }
    }
  });
}

then

var arrayObject= [];
$(document).ready(function () {
   $('#Company').autocomplete({
      source: arrayObject,
      select: function (event, ui) {  alert(ui.item.id); }                                     
   });
});
Vishal Taj PM
  • 1,339
  • 11
  • 23