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: "/ite开发者_如何学Cm/user.php",
minLength: 2,
select: function(event, ui) {
$('#userId').val(ui.item.id);
}
});
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.
精彩评论