[Autocomplete] Problem Inserting Additional Data into the Query

[Autocomplete] Problem Inserting Additional Data into the Query

Hi Guys. I have an inputfield, where I start a autocomplete. The Data Comes from geonames. I also have an additional field, where I have sudden countries. Now i try to change the search parameter depending on this select field, that geonames only gives results of places in this country. Below you'll find my code and an example. I hope you understood, what i want. SIMPLY:

Select a Country (e.g. France) -> Autocomplete (I will not find London, New York or Berlin, but Paris, Toulouse,...)


  1. <script language="JavaScript">
  2.     $(document).ready(function() {
  3.       $("#city").autocomplete("http://ws.geonames.org/searchJSON", {
  4.         dataType: 'jsonp',
  5.         parse: function(data) {
  6.           var rows = new Array();
  7.           data = data.geonames;
  8.           for(var i=0; i<data.length; i++){
  9.             rows[i] = { data:data[i], value:data[i].name, result:data[i].name };
  10.           }
  11.           return rows;
  12.         },
  13.         formatItem: function(row, i, n) {return row.name + ' ('+row.adminName2+'), '+ row.adminName1},
  14.         extraParams: {
  15.           q: '',
  16.           limit: '',
  17.           country: '',
  18.           featureClass: 'P',
  19.           style: 'full',
  20.           lang: 'en',
  21.           maxRows: 10,
  22.           name_startsWith: function () { return $("#city").val() }
  23.         },
  24.         max: 10,
  25.         delay: 10
  26.       }).result(function(e, row) {
  27.         $("#location").val(row.geonameId);
  28.         $("#city").val(row.name + ' ('+row.adminName2+'), '+ row.adminName1);
  29.       });
  30.     });
  31.   </script>
  32.   <input type="hidden" name="location" id="location" value=""/>
  33.   <input id="city" name="city" value="">
  34.   <select id="country" name="country" > 
  35.     <option value="">Select a country</option>
  36.     <option value="AD" >Andorra</option>
  37.     <option value="DE" >Deutschland</option>
  38.     <option value="AF" >Afghanistan</option>
  39.     <option value="AG" >Antigua und Barbuda</option>
  40.     <option value="FR" >Frankreich</option>
  41.     <option value="US" >Amerika</option>
  42.   </select>