Reputation: 555
$(function ()
{
$(".tb").autocomplete({
source: function (request, response) {
$.ajax({
url: "StudentName.asmx/FetchNameList",
data: "{ 'FirstName': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data.d, function (item) {
return {
value: item.Name
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 1
});
});
i want to get the id field from above, i have created a new textbox and inserted $('.id').val(item.ID); but it doesnot return the same id field that i have select from the autocomplete textbox. How can i get that id?
JSON data that i'm getting is like this.
{"d":[{"_type":"Student","ID":1,"Name":"Sagar Khyaju"},{"_type":"Student","ID":2,"Name":"Sagar Khyaju"},{"__type":"Student","ID":4,"Name":"Sagar Lkkk"}]}
Upvotes: 0
Views: 320
Reputation: 555
I found the solution in case anyone needs it. Cheers! :)
$(function () {
$(".tb").autocomplete({
source: function (request, response) {
$.ajax({
url: "StudentName.asmx/FetchNameList",
data: "{ 'FirstName': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data.d, function (item) {
return {
value: item.Name, ID: item.ID
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
select: function (event, ui) {
$(this).val(ui.item.value);
return false;
},
change: function (event, ui) {
$(".student_id").val(ui.item.ID);
return false;
}
});
});
Upvotes: 1
Reputation: 663
Store the jsondata in a variable and you can iterate from the result variable
var result= eval("(" + jsondata + ")");
I guess you are using auto complete
Please have a look at the below link
Upvotes: 0