我正在使用ASP.Net webservice开发JQuery自动完成功能。我有在JQuery ( ASP.Net )中调用JSON webservice的下拉列表,如下所示
$(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
});
});我得到了自动完成的输出,如下所示
{"First":"Steve","Second":"AK"}
{"First":"Evet","Second":"EV"}
{"First":"Stevens","Second":"SV"}如何单独显示“第一个”项(如Steve、Evet、Steve)作为下拉自动完成的输出?
请帮帮我!
发布于 2011-10-11 19:38:23
您需要查看AutoComplete方法上的formatItem选项--试试这个
formatItem: function(row, i, n) {
return row.First;
}发布于 2011-10-11 19:43:13
这可能会起作用。
success: function (data)
{
response($.map(data, function (item)
{
return { label: item.First, value: item.First}
}))
});发布于 2011-10-12 22:50:49
看看我对这个问题的回答:Jquery Autocomplete 2 Fields
但是在字段中使用您的值而不是"A“和"B”。
此外,您(可能)需要一个转换器来处理asp.net数据:
$.ajax({
url: "Webservice.asmx/GetNames",
dataType: "jsond",
type: "POST",
contentType: "application/json",
converters: {
"json jsond": function(msg)
{
return msg.hasOwnProperty('d') ? msg.d : msg;
}
},
...https://stackoverflow.com/questions/7722925
复制相似问题