vendredi 22 octobre 2010

jQuery Autocomplete : set autocomplete field to a value

I struggled to find this and the solution is so simple…

In the select event, you can set the value of the field and if you return false, the value won’t be the selected item.

 $(function() {
$("input#ZipCode").autocomplete({
source: function(request, response) {
$.ajax({
url: '<%= Url.Action("ZipCodeAutoComplete", "Home", new { area = "" }) %>',
data: { term: request.term },
success: function(data) {
response($.map(data, function(item) {
return { label: item.ZipCodel + " - " + item.City, ZipCode: item.ZipCodel, City: item.City }
}))
}
})
},
minLength: 3,
select: function(event, ui) {
$("#City").val(ui.item.Ville);
$("#ZipCode").val(ui.item.ZipCode);
return false;
}
});
});

Aucun commentaire:

Enregistrer un commentaire