0

这是我当前的代码:http: //jsfiddle.net/spadez/nHgMX/2/

  $(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: 7,
        name_startsWith: request.term,
        country: "UK"
      },           
          success: function( data ) {
            response( $.map( data.geonames, function( item ) {
              return {
                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                value: item.name
              }
            }));
          }
        });
      },
      minLength: 1,
      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" );
      }
    });
  });

这个链接说明了这一点:http ://www.geonames.org/export/geonames-search.html

要素类(默认=所有要素类);该参数可能出现多次,例如:featureClass=P&featureClass=A

如何调整我的代码以将要素类设置为 A 和 P 而不仅仅是当前的 P?

4

1 回答 1

1

我找到了2个解决方案:

第一个是@George Cummins 建议的使用方法:

jQuery.ajaxSettings.traditional = true; 

...

$.ajax({
          url: "http://ws.geonames.org/searchJSON",
          dataType: "jsonp",
       data: {
        featureClass: ["A","P"],

其次,您可以将参数添加到查询字符串:

$.ajax({
          url: "http://ws.geonames.org/searchJSON?featureClass=A&featureClass=P", // you can concantenate it
于 2013-07-01T20:47:35.247 回答