简体   繁体   English

通过XML进行JQuery自动完成(动态结果)

[英]JQuery AutoComplete From XML (Dynamic Results)

Perhaps I'm not understanding this concept (I'm an AJAX/javascript/Web newbie). 也许我不理解这个概念(我是AJAX / javascript / Web新手)。 I'm using the JQuery autocomplete feature and if I specify a small, limited items flat file (suggestions.xml) the function works fine, but when I use an actual production data file (3 MB) of suggestions the script doesn't work at all. 我使用的是JQuery自动完成功能,如果我指定了一个有限的小型项目平面文件(suggestions.xml),该功能就可以正常工作,但是当我使用建议的实际生产数据文件(3 MB)时,该脚本将不起作用完全没有

So I created a web service that generates XML based on the characters in the textbox but it appears this JQuery doesn't run on each keypress, rather only when the page first loads. 因此,我创建了一个Web服务,该服务基于文本框中的字符生成XML,但看来此JQuery不会在每次按键时都运行,而仅在首次加载页面时运行。 Obviously, for this function to be of any use it needs to fetch results dynamically as the user types into the input field. 显然,要使该功能有用,它需要在用户键入输入字段时动态获取结果。

$(document).ready(function () {
var myArr = [];

$.ajax({
    type: "GET",
    // this line always sends the default text; not what the user is typing
    url: "Suggestions.aspx?searchString=" + $("input#txtSearch").val(),
    dataType: "xml",
    success: parseXml,
    complete: setupAC,
    failure: function (data) {
        alert("XML File could not be found");
    }
});

function parseXml(xml) {
    //find every query value
    $(xml).find("result").each(function () {
        myArr.push({ url: $(this).attr("url"), label: $(this).attr("name") + ' (' + $(this).attr("type").toLowerCase() + ')' });
    });
}

function setupAC() {
    $("input#txtSearch").autocomplete({
        source: myArr,
        minLength: 3,
        select: function (event, ui) {
            $("input#txtSearch").val(ui.item.label);
            window.location.href = ui.item.url;
            //alert(ui.item.url + " - " + ui.item.label);
        }
    });
}

}); });

On the server I expected to see a few requests corresponding to the characters the user is typing into the search box but instead I get a single message: 在服务器上,我希望看到一些与用户在搜索框中键入的字符相对应的请求,但是却收到一条消息:

2013-10-18 22:02:04,588 [11] DEBUG baileysoft.mp3cms.Site - QueryString params: [searchString]:'Search'

My flat file of suggestions appears to be way to large for JQuery to handle and my web service script is never called, except when the page is first loaded. 我的平面建议文件似乎是供JQuery处理的大型文件,并且从未调用过我的Web服务脚本,除非首次加载页面时。

How do I generate suggestions dynamically as the user is typing in the search box if I can't run back to the database (via my web service) to fetch suggestions as the user is typing? 如果我无法返回数据库(通过Web服务)以在用户键入内容时获取建议,那么当用户在搜索框中键入内容时,如何动态生成建议?

Ok, I got it all worked out. 好吧,我知道了。

On the ASPNET side; 在ASPNET方面; I created a form to receive and respond to the AJAX: 我创建了一个表单来接收和响应AJAX:

    Response.ContentType = "application/json";
    var term = Request.Form["term"];

    var suggestions = GetSuggestions(term); // Database results
    if (suggestions.Count < 1)
        return;

    var serializer = new JavaScriptSerializer();
    Response.Write(serializer.Serialize(suggestions);

On the AJAX side I modified the js function: 在AJAX方面,我修改了js函数:

    $("input#txtSearch").autocomplete({
       minLength: 3,
       source: function (request, response) {
         $.ajax({
           url: "Suggestions.aspx",
           data: { term: $("input#txtSearch").val() },
           dataType: "json",
           type: "POST",
           success: function (data) {
            response($.map(data, function (obj) {
                return {
                    label: obj.Name,
                    value: obj.Url
                };
            }));
           }
          });
       },
       select: function (event, ui) {
         $("input#txtSearch").val(ui.item.label);
         window.location.href = ui.item.value;
       }
    });

Everything is working as expected now. 现在一切都按预期进行。

Hope this helps someone else who might get stuck trying to figure out JQuery stuff for ASPNET. 希望这对试图找出ASPNET的JQuery内容的其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM