Jquery AutoComplete 插件调用

发布于 2024-08-24 12:05:11 字数 1043 浏览 5 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2024-08-31 12:05:11

首先,你的语法看起来与我习惯的不同。如果您使用的是 jQuery UI 中的自动完成小部件,那么自动完成语法如下所示:

$("#input1").autocomplete({
      source: "http://localhost/Whatever"
});

那么也许您没有使用 jQuery UI 中包含的自动完成功能?

如果你是......
根据 jQuery UI 自动完成的文档,源可以是以下三者之一:数组、字符串 (URL) 或函数。如果是数组,可以是对象,也可以是单词。如果是对象,则每个对象都应该公开一个 labelvalue 属性,或两者都公开。

如果它是一个 URL,那么它应该返回符合其中一种数组格式的 JSON。例如,它应该返回

[ "albatross", "bison", "cayman", "duck", ...] 

[ { "label": "albatross", "value": "72" }, 
  { "label": "bison", "value": "24" }, 
   ...
]

最有可能您正在检索不符合上述格式之一的内容。

另请参阅此答案

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"
});

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 a value 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

[ "albatross", "bison", "cayman", "duck", ...] 

or

[ { "label": "albatross", "value": "72" }, 
  { "label": "bison", "value": "24" }, 
   ...
]

Most likely you are retrieving something that does not conform to one of the above formats.

See also, this answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文