JQuery Autocomplete with ASp.Net webservice
I am working on the JQuery Autocomplete using ASP.Net webservice. I have ASP.Net webservice being called in JQuery (JSON) drop down as
$(document).ready(function () {
$("#txtTest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Webservice.asmx/GetNames",
data: "{'prefix':'" + request.term + "'}",
dataType: "json",
async: true,
success: function (data){
response($.map(data, function(item)
{ return item ; }));
},
error: function (result) {
alert("Due to unexpected errors we were unable to load data");
}
});
},
minLength:2
});
});
And i am getting the output on the drop of auto-complete as
{"First":"Steve","Second":"AK"}
{"First":"Evet","Second":"EV"}
{"First":"Stevens","Second":"SV"}
How do i display the "First" items alone (Like Steve, Evet, 开发者_如何学CStevens) as the output of the drop down auto-complete?
Please help me!
You need to look at the formatItem option on the AutoComplete method - try this
formatItem: function(row, i, n) {
return row.First;
}
this would probably do the trick
success: function (data)
{
response($.map(data, function (item)
{
return { label: item.First, value: item.First}
}))
});
Take a look at my answer to this one: Jquery Autocomplete 2 Fields
but use your values instead of "A" and "B" in the fields.
In addition, you (might) need a converter to handle the asp.net data:
$.ajax({
url: "Webservice.asmx/GetNames",
dataType: "jsond",
type: "POST",
contentType: "application/json",
converters: {
"json jsond": function(msg)
{
return msg.hasOwnProperty('d') ? msg.d : msg;
}
},
...
精彩评论