Jquery AutoComplete 插件调用
当我使用 JQuery 的自动完成功能并对页面中的数组值进行硬编码时,效果非常好;但我需要做的是从 Web 服务或控制器内的公共函数获取数组值。我尝试了各种方法,但似乎无法使其发挥作用。我得到的最远是将数据拉入一个长字符串,当提供自动完成结果时,它是匹配的长字符串,我明白为什么。
$("#TaskEmailNotificationList").autocomplete("http://localhost/BetterTaskList/Accounts/registeredUsersEmailList", {
multiple: true,
mustMatch: false,
multipleSeparator: ";",
autoFill: true
});
有人遇到过这个吗?我正在使用 C#。
更新: 下面的代码是向前迈出的一步,我现在返回了一个数组,但我认为我在页面上处理它是错误的。
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);
}
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
});
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,你的语法看起来与我习惯的不同。如果您使用的是 jQuery UI 中的自动完成小部件,那么自动完成语法如下所示:
那么也许您没有使用 jQuery UI 中包含的自动完成功能?
如果你是......
根据 jQuery UI 自动完成的文档,源可以是以下三者之一:数组、字符串 (URL) 或函数。如果是数组,可以是对象,也可以是单词。如果是对象,则每个对象都应该公开一个
label
或value
属性,或两者都公开。如果它是一个 URL,那么它应该返回符合其中一种数组格式的 JSON。例如,它应该返回
或
最有可能您正在检索不符合上述格式之一的内容。
另请参阅此答案
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:
So maybe you are not using the autocomplete that is included in jQuery UI?
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 avalue
property or both.If it is a URL, then it should return JSON that conforms to one of the array formats. Eg, it should return
or
Most likely you are retrieving something that does not conform to one of the above formats.
See also, this answer