【问题标题】:Jquery mobile autocomplete and GeonamesJquery 移动自动完成和 Geonames
【发布时间】:2013-07-11 19:00:17
【问题描述】:

我正在尝试使用自动完成字段来查找位置,我看到此处显示的基于 Geobytes 数据库的 Jquery Mobile 示例: http://view.jquerymobile.com/1.3.1/dist/demos/widgets/autocomplete/autocomplete-remote.html

$( document ).on( "pageinit", "#myPage", function() {
$( "#autocomplete" ).on( "listviewbeforefilter", function ( e, data ) {
    var $ul = $( this ),
        $input = $( data.input ),
        value = $input.val(),
        html = "";
    $ul.html( "" );
    if ( value && value.length > 2 ) {
        $ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
        $ul.listview( "refresh" );
        $.ajax({
            url: "http://gd.geobytes.com/AutoCompleteCity",
            dataType: "jsonp",
            crossDomain: true,
            data: {
                q: $input.val()
            }
        })
        .then( function ( response ) {
            $.each( response, function ( i, val ) {
                html += "<li>" + val + "</li>";
            });
            $ul.html( html );
            $ul.listview( "refresh" );
            $ul.trigger( "updatelayout");
        });
    }
});
});

但我会使用 Geonames 服务,因为它包含最大的数据库。这是 JqueryUI 自动完成的示例: http://jqueryui.com/autocomplete/#remote-jsonp

  $(function() {
function log( message ) {
  $( "<div>" ).text( message ).prependTo( "#log" );
  $( "#log" ).scrollTop( 0 );
}

$( "#city" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "http://ws.geonames.org/searchJSON",
      dataType: "jsonp",
      data: {
        featureClass: "P",
        style: "full",
        maxRows: 12,
        name_startsWith: request.term
      },
      success: function( data ) {
        response( $.map( data.geonames, function( item ) {
          return {
            label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
            value: item.name
          }
        }));
      }
    });
  },
  minLength: 2,
  select: function( event, ui ) {
    log( ui.item ?
      "Selected: " + ui.item.label :
      "Nothing selected, input was " + this.value);
  },
  open: function() {
    $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
  },
  close: function() {
    $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
  }
});
});

我正在尝试结合这两个示例,但没有成功... :(我可以帮忙吗? 提前致谢!

【问题讨论】:

    标签: json jquery-mobile autocomplete geonames


    【解决方案1】:

    解决了! :) 这是完整的工作代码:

            $( document ).on( "pageinit", "#myPage", function() {
            $( "#autocomplete" ).on( "listviewbeforefilter", function ( e, data ) {
                var $ul = $( this ),
                    $input = $( data.input ),
                    value = $input.val(),
                    html = "";
                $ul.html( "" );
                if ( value && value.length > 2 ) {
                    $ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
                    $ul.listview( "refresh" );
                    $.ajax({
                        url: "http://ws.geonames.org/searchJSON",
                        dataType: "jsonp",
                        crossDomain: true,
                        data: {
                            featureClass: "P",
                            style: "full",
                            maxRows: 12,
                            lang: "it",
                            name_startsWith: $input.val()
                        }
                    })
                    .then( function ( response ) {
                        $.each( response.geonames, function ( i, val ) {
                            html += "<li>" + val.name + (val.adminName1 ? ", " + val.adminName1 : "") + ", " + val.countryName + "</li>";
                        });
                        $ul.html( html );
                        $ul.listview( "refresh" );
                        $ul.trigger( "updatelayout");
                    });
                }
            });
        });
    

    希望对您有所帮助! ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2011-06-19
      • 1970-01-01
      • 2011-06-08
      相关资源
      最近更新 更多