jquery:使用远程 xml 源自动完成

发布于 2024-09-29 13:06:14 字数 1350 浏览 1 评论 0原文

我正在尝试实现一个自动完成文本框,其值由返回 XML 内容的远程脚本生成。我正在使用 JQuery-1.4.3 和 JQuery-UI-1.8.5 中的自动完成小部件。

我已经研究了 XML 数据解析一次 示例的自动完成演示页面,并且正在尝试落实意见:

这也应该作为如何解析远程 XML 数据源的参考 - 解析只会针对源回调中的每个请求进行。

作为测试,我尝试使用提供的 XML 演示来实现此功能。上面的评论表明自动完成“源”属性必须替换为 Ajax 调用。然而,当我在 演示页面 提供的函数中修改此内容时,我不使用以下自动完成功能获得任何结果:

$( "#birds" ).autocomplete({
  source: function(request, response) {
    $.ajax({
      url: "london.xml",
      dataType: "xml",
      success: function( xmlResponse ) {
        var data = $( "geoname", xmlResponse ).map(function() {
//alert($('name', this).text());
          return {
            value: $( "name", this ).text() + ", " +
                   ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
            id: $( "geonameId", this ).text()
          };
        }).get();
      }
    })
  },
  minLength: 0,
  select: function( event, ui ) {
    log( ui.item ?
         "Selected: " + ui.item.value + ", geonameId: " + ui.item.id :
         "Nothing selected, input was " + this.value );
  }
});

不过,注释掉简单的调试弹出消息表明 Ajax 调用确实设法检索用于构造数据的值。我的错误在哪里?

非常感谢任何帮助!

亲切的问候,

罗恩·范登布兰登

I am trying to implement an autocomplete textbox whose values are generated by a remote script returning XML contents. I'm using JQuery-1.4.3 and the autocomplete widget from JQuery-UI-1.8.5.

I've studied the autocomplete demo page for the XML data parsed once example, and am trying to implement the comments:

This should also serve as a reference on how to parse a remote XML datasource - the parsing would just happen for each request within the source-callback.

As a test, I'm trying to get this working with the supplied XML demo. Above comment suggests that the autocomplete 'source' property has to be replaced with the Ajax call. Yet, when I modify this in the function supplied at the demo page, I'm not getting any results with following autocomplete function:

$( "#birds" ).autocomplete({
  source: function(request, response) {
    $.ajax({
      url: "london.xml",
      dataType: "xml",
      success: function( xmlResponse ) {
        var data = $( "geoname", xmlResponse ).map(function() {
//alert($('name', this).text());
          return {
            value: $( "name", this ).text() + ", " +
                   ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
            id: $( "geonameId", this ).text()
          };
        }).get();
      }
    })
  },
  minLength: 0,
  select: function( event, ui ) {
    log( ui.item ?
         "Selected: " + ui.item.value + ", geonameId: " + ui.item.id :
         "Nothing selected, input was " + this.value );
  }
});

Still, commenting out the simple debug popup message shows that the Ajax call does manage to retrieve the values used in constructing the data. Where's my mistake?

Any help much appreciated!

Kind regards,

Ron Van den Branden

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

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

发布评论

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

评论(1

不爱素颜 2024-10-06 13:06:14

好吧,

我已经找到了解决方案,并且可以很高兴地回答自己。

在我最初的尝试中,我在 Ajax 函数的成功回调中声明了变量“data”,但没有对它执行任何操作。解决方案很简单:添加一个response() 函数,如果ajax 调用成功,该函数将返回数据变量。我将添加完整的更正函数(尽管唯一的更改位于第 14 行):
当然,在这种情况

$( "#birds" ).autocomplete({
   source: function(request, response) {
     $.ajax({
       url: "london.xml",
       dataType: "xml",
       success: function( xmlResponse ) {
         var data = $( "geoname", xmlResponse ).map(function() {
           return {
             value: $( "name", this ).text() + ", " +
                     ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
             id: $( "geonameId", this ).text()
           };
         });
       response(data);
       }
     })
   },
   minLength: 0,
   select: function( event, ui ) {
     log( ui.item ?
       "Selected: " + ui.item.value + ", geonameId: " + ui.item.id :
       "Nothing selected, input was " + this.value );
   }
 });

下可以直接构造响应,而无需首先声明数据变量:
复制代码

     $.ajax({
       url: "london.xml",
       dataType: "xml",
       success: function( xmlResponse ) {
         response($( "geoname", xmlResponse ).map(function() {
           return {
             value: $( "name", this ).text() + ", " +
                     ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
             id: $( "geonameId", this ).text()
           };
         }));
       }
     })
   }

(注意:这些函数片段在插入'remote XML'自动完成演示时起作用)

唷!现在开始用这个做一些有用的事情。

罗恩

Ok,

I've found the solution, and can gladly answer myself.

In my original attempt, I declared the variable 'data' in the success callback for the Ajax function, but didn't do anything with it. The solution is simple enough: add a response() function that will return the data variable if the ajax call is successful. I'll add the complete corrected function (though the sole change is on line 14):
Copy code

$( "#birds" ).autocomplete({
   source: function(request, response) {
     $.ajax({
       url: "london.xml",
       dataType: "xml",
       success: function( xmlResponse ) {
         var data = $( "geoname", xmlResponse ).map(function() {
           return {
             value: $( "name", this ).text() + ", " +
                     ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
             id: $( "geonameId", this ).text()
           };
         });
       response(data);
       }
     })
   },
   minLength: 0,
   select: function( event, ui ) {
     log( ui.item ?
       "Selected: " + ui.item.value + ", geonameId: " + ui.item.id :
       "Nothing selected, input was " + this.value );
   }
 });

Of course, in this case the response can be constructed directly, without first declaring a data variable:
Copy code

     $.ajax({
       url: "london.xml",
       dataType: "xml",
       success: function( xmlResponse ) {
         response($( "geoname", xmlResponse ).map(function() {
           return {
             value: $( "name", this ).text() + ", " +
                     ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
             id: $( "geonameId", this ).text()
           };
         }));
       }
     })
   }

(note: these function snippets work when inserted in the 'remote XML' autocomplete demo)

Phew! Now on to doing something useful with this.

Ron

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