【发布时间】:2015-03-06 14:21:27
【问题描述】:
无论我如何返回数据,作为 List 或 JSON 字符串,jQuery Autocomplete 插件都不会显示下拉列表中的值。
Javascript:
$("#myText").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
url: "Default.aspx/GetList",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{'term':'" + request.term + "'}",
success: function (data) {
response(data);
},
error: function (xhr, error) {
console.debug(xhr); console.debug(error);
}
});
},
minLength: 0,
select: function (event, ui) {
var result = ui.item.id;
}
});
服务器端 (.aspx):
[ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod]
public static string GetList(string term)
{
List<string> list = new List<string>();
list.Add("apple");
list.Add("apricot");
list.Add("apple cider");
string json = "[" + string.Join(",",
list.Select(i =>
"{ 'id': '" + i + "'" + "}"
)) + "]";
return json;
}
我附上了两张图片,第一张是在浏览器中的显示方式,第二张是javascript中的断点。
我做错了什么?
【问题讨论】:
标签: jquery jquery-autocomplete