jQuery AutoComplete插件调用 [英] Jquery AutoComplete Plugin calling
问题描述
当我使用JQuery的自动完成功能并对页面中的数组值进行硬编码时,它的工作效果非常好;但是我需要做的是从Web服务或控制器内部的公共函数获取数组值.我尝试了各种方法,但似乎无法使其正常工作.我得到的最远的结果是将数据拖入一个长字符串中,当提供自动完成结果时,它就是匹配的长字符串,这我理解为什么.
When I use JQuery's autocomplete and hardcode the array values in the page it works wonderful; but what I need to do is obtain the array values from either a web service or from a public function inside a controller. I have tried various way and can't seem to make it work. The farthest I got is pulling the data in to a long string and when the auto complete results are provided it's the long string which matches, which I understand why.
$("#TaskEmailNotificationList").autocomplete("http://localhost/BetterTaskList/Accounts/registeredUsersEmailList", {
multiple: true,
mustMatch: false,
multipleSeparator: ";",
autoFill: true
});
有人遇到过这个吗?我正在使用C#.
has anyone encountered this? I am using C#.
更新: 下面的代码是向前的一步,我现在返回一个数组,但是我认为我在页面上处理的错误.
UPDATE: The below code is a step forward I am now getting an array returned but I think I'm processing it wrong on my page.
var emailList = "http://localhost/BetterTaskList/Account/RegisteredUsersEmailList";
$("#TaskEmailNotificationList").autocomplete(emailList, {
multiple: true,
mustMatch: false,
multipleSeparator: ";",
autoFill: true
});
[HttpGet]
public ActionResult RegisteredUsersEmailList()
{
BetterTaskListDataContext db = new BetterTaskListDataContext();
var emailList = from u in db.Users select u.LoweredUserName;
return Json(emailList.ToList(), JsonRequestBehavior.AllowGet);
}
推荐答案
首先,您的语法看起来与我惯用的语法不同.如果您使用的是jQuery UI的自动完成小部件,则自动完成语法如下:
First, your syntax looks different than I am used to. If you are using the autocomplete widget that is part of jQuery UI, then the autocomplete syntax is like this:
$("#input1").autocomplete({
source: "http://localhost/Whatever"
});
因此,也许您未使用jQuery UI中包含的自动完成功能?
So maybe you are not using the autocomplete that is included in jQuery UI?
如果你是....
根据关于jQuery UI自动完成的文档,来源可能是以下三点之一:数组,字符串(URL)或函数.如果是数组,则可以是对象或单词.如果是对象,则每个对象都应公开label
或value
属性或两者.
In case you are....
According to the documentation for jQuery UI autocomplete, the source can be one of three things; an array, a string (URL), or a function. If it is an array, it can be objects or words. If objects, then each should expose either a label
, or a value
property or both.
如果它是一个URL,则它应返回符合一种数组格式的JSON.例如,它应该返回
If it is a URL, then it should return JSON that conforms to one of the array formats. Eg, it should return
[ "albatross", "bison", "cayman", "duck", ...]
或
[ { "label": "albatross", "value": "72" },
{ "label": "bison", "value": "24" },
...
]
您很可能正在检索与上述格式之一不符的内容.
Most likely you are retrieving something that does not conform to one of the above formats.
另请参见,此答案
这篇关于jQuery AutoComplete插件调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!