Jquery AutoComplete Plugin calling

三世轮回 提交于 2019-12-11 07:05:17

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/2421468/jquery-autocomplete-plugin-calling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!