Jquery autocomplete how to get id

I am trying to use jQuery autocomplete and should get the id of the value that was filled in the field. I am using a hidden element to store the id value in my html. Autocomplete works very well and shows the results until I add the identifier to the array and then stop showing the results.

This is an example of what the server code returns.

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

This is jquery

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

You suggest adding 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 } }); }, }); 

Thus, the value in the selection option is userId, and text is userName.

Set a breakpoint when choosing a function and check the value of 'ui' on firebug .

+5
source

Source: https://habr.com/ru/post/1332469/


All Articles