jquery 自动完成源作为接受查询字符串的 url

发布于 2024-11-09 10:03:27 字数 2077 浏览 6 评论 0原文

我正在尝试在文本框中使用 jQuery Autocomplete UI 小部件,但我没有运气让源代码正常工作。我有一个充满名称的数据库,我希望自动完成功能能够对其进行处理,因此我创建了一个名为 searchpreload.aspx 的页面,它在 url 中查找变量并根据查询字符串 vraiable 查询数据库。

当我在搜索框中键入内容时,我使用 keyup 函数,这样我就可以捕获需要发送的值。然后我从数据库收集字符串:

 if (Request.QueryString["val"] != null)
        {
            curVal = Request.QueryString["val"].ToString();
            curVal = curVal.ToLower();
            if (Request.QueryString["Type"] != null)
                type = Request.QueryString["Type"].ToString();

            SwitchType(type,curVal);
        }

它正确查询数据库,然后获取字符串并将它们放入列表中并将它们打印到页面:

private void PreLoadStrings(List<string> PreLoadValues, string curVal)
    {
        StringBuilder sb = new StringBuilder();
        if (PreLoadValues.Any())
        {
            foreach (string str in PreLoadValues)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    if (str.ToLower().Contains(curVal))
                        sb.Append(str).Append("\n");
                }
            }
            Response.Write(sb.ToString());
        }
    }

这工作正常,如果我导航到此页面,我会得到一个列表我需要的所有数据,但是我无法让它显示在自动完成框中。当我调试代码时,自动完成的来源每次都正确调用此页面并获取正确的数据,它只是不显示任何内容。我做错了什么吗?

JQuery 代码:

<script type="text/javascript">
          $(document).ready(function () {
              $(".searchBox").focus();
              var checked = 'rbCNumber';
              $("input:radio").change(function (eventObject) {
                  checked = $(this).val();
              });
              $(".searchBox").keyup(function () {
                  var searchValue = $(".searchBox").val();
                  //alert("Searchpreload.aspx?val=" + searchValue + "&Type=" + checked);
                  $(".searchBox").autocomplete({
                      source:"Searchpreload.aspx?val=" + searchValue + "&Type=" + checked,
                      minLength: 2


                  });
              });

          });      
  </script>

另外,我应该以不同的方式执行此操作以使其更快吗?

I am trying to use the jQuery Autocomplete UI widget on a text box and I am having no luck getting the source to work. I have a database full of names that I want the autocomplete to work against, so I created a page called searchpreload.aspx and it looks for a variable in the url and queries the db based on the querystring vraiable.

When I type in the search box, I am using the keyup function so I can capture what the value is that needs to be sent over. Then I do my string gathering from the db:

 if (Request.QueryString["val"] != null)
        {
            curVal = Request.QueryString["val"].ToString();
            curVal = curVal.ToLower();
            if (Request.QueryString["Type"] != null)
                type = Request.QueryString["Type"].ToString();

            SwitchType(type,curVal);
        }

It queries the database correctly and then it takes the strings and puts them in a list and prints them out to the page:

private void PreLoadStrings(List<string> PreLoadValues, string curVal)
    {
        StringBuilder sb = new StringBuilder();
        if (PreLoadValues.Any())
        {
            foreach (string str in PreLoadValues)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    if (str.ToLower().Contains(curVal))
                        sb.Append(str).Append("\n");
                }
            }
            Response.Write(sb.ToString());
        }
    }

This works fine, if I navigate to this page I get a listing of all of the data that I need, however I can not get it to show up in the autocomplete box. When I debug the code, the source of the autocomplete is calling this page correctly each time and getting the correct data, it just is not displaying anything. Am I doing something wrong?

JQuery Code:

<script type="text/javascript">
          $(document).ready(function () {
              $(".searchBox").focus();
              var checked = 'rbCNumber';
              $("input:radio").change(function (eventObject) {
                  checked = $(this).val();
              });
              $(".searchBox").keyup(function () {
                  var searchValue = $(".searchBox").val();
                  //alert("Searchpreload.aspx?val=" + searchValue + "&Type=" + checked);
                  $(".searchBox").autocomplete({
                      source:"Searchpreload.aspx?val=" + searchValue + "&Type=" + checked,
                      minLength: 2


                  });
              });

          });      
  </script>

Also, should I be doing this a different way to make it faster?

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

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

发布评论

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

评论(1

喜你已久 2024-11-16 10:03:27

您不会将结果显示到任何内容中 - 源将返回一个数据项,然后您可以使用该数据项填充页面上的其他内容。查看自动完成的选择和焦点方法。

这是我如何做到这一点的一个示例:

field.autocomplete({
                minLength: 1,
                source: "whatever",
                focus: function (event, ui) {
                    field.val(ui.item.Id);
                    return false;
                },
                search: function (event, ui) {
                    addBtn.hide();
                },
                select: function (event, ui) {
                    setup(ui);
                    return false;
                }
            })
            .data("autocomplete")._renderItem = function (ul, item) {
                return $("<li></li>")
                .data("item.autocomplete", item)
                .append("<a>" + item.Id+ ", " + item.Name + "</a>")
                .appendTo(ul);
            };

.data 部分是您缺少的部分。一旦数据从自动完成中返回,您就不会用它做任何事情。

源不需要包含用户在搜索框中输入的术语。 Jquery 会自动将术语附加到您的查询字符串中。如果您观察 Firebug 中生成的请求,您将看到术语“查询”挂在 url 的末尾。

You arent displaying the results into anything - source will return a data item that you can then use to populate something else on the page. Look at autocomplete's select and focus methods.

here is an example of how i have done it:

field.autocomplete({
                minLength: 1,
                source: "whatever",
                focus: function (event, ui) {
                    field.val(ui.item.Id);
                    return false;
                },
                search: function (event, ui) {
                    addBtn.hide();
                },
                select: function (event, ui) {
                    setup(ui);
                    return false;
                }
            })
            .data("autocomplete")._renderItem = function (ul, item) {
                return $("<li></li>")
                .data("item.autocomplete", item)
                .append("<a>" + item.Id+ ", " + item.Name + "</a>")
                .appendTo(ul);
            };

The .data part is the part you are missing. Once the data comes back from the autocomplete you arent doing anything with it.

The source does not need to include the term the user entered into the search box. Jquery will automatically append the term onto the query string for you. If you watch the request get generated in firebug, you will see the term query hanging off the end of the url.

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