jquery ui 自动完成需要额外的功能

发布于 2024-10-08 10:20:57 字数 1686 浏览 4 评论 0原文

我有这样的自动完成代码:

$("input#PickupSpot").autocomplete({
  source: function(request, response){
     $.ajax({
        url: "AjaxSearch.aspx",
        dataType: "jsonp",
        data: {
           a: "getspots",
           c: "updateSpotList",
           q: request.term
        },
        success: function(){
           alert("Success");
        },
        error: function (xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                alert(thrownError);
        }

     });
  }

});

当我尝试获取数据时,Firebug 显示错误:“updateSpotList 未定义”。 我需要创建一个名为 updateSpotList 的函数来获取服务器的响应。并且永远不会调用成功警报。

为什么我需要这个功能?也许aspx中定义了一些东西?这是:

string response = "";

 string callback = Request.QueryString["c"];
 string action = Request.QueryString["a"]; 
 string query = Request.QueryString["q"];

  //Ensure action parameter is set
  if(action != null && action.Equals("getspots")){
        //Ensure query parameter is set
        if (query != null && query.Length > 0){
            SpotListRequest request = new SpotListRequest
            {
                FreeText = query,
                Language = "sv-SE"
            };
            IEnumerable<Spot> spots = DataBridge.GetSpotList(null, null, query).OrderBy(i => i.FullName);

            JavaScriptSerializer js = new JavaScriptSerializer();

            string json = js.Serialize(spots.ToArray());

            //Ensure callback parameter is set
            if (callback != null && callback.Length > 0)
            {
                response = String.Format("{0}('{1}')", callback, json);
            }
        }
    }

I have such autocomplete code:

$("input#PickupSpot").autocomplete({
  source: function(request, response){
     $.ajax({
        url: "AjaxSearch.aspx",
        dataType: "jsonp",
        data: {
           a: "getspots",
           c: "updateSpotList",
           q: request.term
        },
        success: function(){
           alert("Success");
        },
        error: function (xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                alert(thrownError);
        }

     });
  }

});

When i try to get data Firebug shows an error: "updateSpotList is not defined".
I need to creat a function called updateSpotList to get response from the server. And the success alert is never invoked.

Why do i need this function? Maybe there are something defined in aspx? It is:

string response = "";

 string callback = Request.QueryString["c"];
 string action = Request.QueryString["a"]; 
 string query = Request.QueryString["q"];

  //Ensure action parameter is set
  if(action != null && action.Equals("getspots")){
        //Ensure query parameter is set
        if (query != null && query.Length > 0){
            SpotListRequest request = new SpotListRequest
            {
                FreeText = query,
                Language = "sv-SE"
            };
            IEnumerable<Spot> spots = DataBridge.GetSpotList(null, null, query).OrderBy(i => i.FullName);

            JavaScriptSerializer js = new JavaScriptSerializer();

            string json = js.Serialize(spots.ToArray());

            //Ensure callback parameter is set
            if (callback != null && callback.Length > 0)
            {
                response = String.Format("{0}('{1}')", callback, json);
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

若言繁花未落 2024-10-15 10:20:57

您可以指定 URL 作为源参数,而不是数据参数。

http://jqueryui.com/demos/autocomplete/#option-source

该插件将向您的服务器发出请求,您应该返回一个如下所示的 JSON 数组:

[{label: "Name"}, {label: "Name 2"}]

将您的 JavaScript 代码更改为:

$("input#PickupSpot").autocomplete({
  source: "AjaxSearch.aspx?a=getspots&c=updateSpotList"
});

自动完成插件将附加一个将名为 term 的参数添加到源 URL,并使用输入元素的当前值,因此如果您在输入元素中编写了 test,则将发出请求:“AjaxSearch.aspx?a=getspots&c=updateSpotList&term=test” 。

在服务器上,您希望将 q 更改为 term。

You can specify a URL as the source parameter, instead of the data parameter.

http://jqueryui.com/demos/autocomplete/#option-source

The plugin will make a request to your server, and you should return a JSON array looking like this:

[{label: "Name"}, {label: "Name 2"}]

Change your JavaScript code to this:

$("input#PickupSpot").autocomplete({
  source: "AjaxSearch.aspx?a=getspots&c=updateSpotList"
});

The autocomplete plugin will append a parameter named term to the source URL, with the current value of the input element, so a request would be made to: "AjaxSearch.aspx?a=getspots&c=updateSpotList&term=test" if you wrote test in the input element.

On the server, you want to change q to term.

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