Reputation: 275
I am trying to get a list of name and Id at autocomplete. I have keypress event at textbox and called a funstion of ajax post and I am able to get list for selection. I am trying to get Id and Name. If I have binded name in the textbox then where should I have to keep ID so that it could not be visible to user but I can use it when I have to save data. I can use hidden field but how to assign that Id to hidden field id if Select event of Autocomplete is not working. Also, I need to change the hidden field value when another element is get selected from list. Please Help me regarding this. Thank You.
function SearchClients() {
}
$(document).ready(function () {
$("#txt_Autocomplete").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "../PsychiatricEvaluation/SearchClients",
data: "{'searchtext':'" + document.getElementById('txt_Autocomplete').value + "'}",
dataType: "json",
success: function (data) {
response($.map(data.Data, function (item) {
return {
label: item.Name,
value: item.id
}
}));
},
select: function (event, ui) {
alert("hi");
//$("#txt_Autocomplete").val(ui.item.value);
$("#hdnPkClientId").val(ui.item.id);
},
change: function (e, ui) {
alert("changed!");
},
error: function (result) {
alert('Error');
}
});
}
});
});
Upvotes: 1
Views: 1883
Reputation: 67898
Put your jQuery hookups inside of the $(document).ready
event:
$(document).ready(function () {
$("#txt_Autocomplete").autocomplete({ ...
});
Upvotes: 1