Setting Google Places 'Types' on Dropdown Input

强颜欢笑 提交于 2019-12-24 07:15:15

问题


I'm using the Google Places API to list pick up and drop off locations for a transportation service. We have two fields that, when typed in, autosuggest results.

When a a dropdown, #transport-type, is set to the first option, value = 1, then the input field, #pick-up-location, should have 'types' airport added to its results.

JS:

function transport_types() {
 if ($('#transport-type').val() == 1) { 
     var input = document.getElementById('pick-up-location');
     var options = { types: ['airport'] };
     autocomplete = new google.maps.places.Autocomplete(input, options);
 }
 if ($('#transport-type').val() == 2) {
     var input = document.getElementById('drop-off-location');
     var options = { types: ['airport'] };
     autocomplete = new google.maps.places.Autocomplete(input, options);
 }
 google.maps.event.addDomListener(window, 'load', initialize);
}

Dropdown HTML:

<select id="transport-type" name="TransNeeded" required="true" tabindex="101" onChange="transport_types();">
  <option value='0' selected='selected'>Where do you want to go?</option>
  <option value='1' onfocus="enableGoogle('', true);"  id="pick-up-type">Pick-Up Location is an Airport</option>
  <option value='2' onfocus="enableGoogle('', true);" id="drop-off-type">Drop-Off Location is an Airport</option>
  <option value='3' onfocus="enableGoogle('', true);" id="point-point-type">Point to Point or Charter</option>
</select>    

Input HTML:

<input type="text" id="pick-up-location" name="PUFullAddress" tabindex="104" value="Pick-Up Location" required="true" 
  onblur="if(this.value.length==0) { $('#pick-up-location').attr('value', ''); }"
  onkeypress="delay(50);" 
  onfocus="enableGoogle('PU', false);" 
/>

<input id="drop-off-location" name="DOFullAddress" type="text" tabindex="105" value="Drop-Off Location" required="true" 
 onblur="if(this.value.length==0) { $('#drop-off-location').attr('value', ' '); }"
 onkeypress="delay(50);" 
 onfocus="enableGoogle('DO', false);" 
/>

I'm not the most seasoned jQuery programmer, so it could even be an issue of syntax. I appreciate any insight you guys can provide.

Thanks for your time!


回答1:


You're missing a close bracket and have an extra ; Try the following

 if ($('#transport-type').attr('value', '1')) {


来源:https://stackoverflow.com/questions/12690865/setting-google-places-types-on-dropdown-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!