1 つの入力フィールドに 2 つのイベント リスナーがあります。1 つは変更時に呼び出され、もう 1 つは Google のオートコンプリート ドロップダウンをクリックしたときに呼び出されます。私の問題は、このようなオートコンプリートをクリックすると、両方のハンドラーが呼び出され、Google API にリクエストを送信することです。
onchange イベントをバインドして、Google オートコンプリート ajax 呼び出しが発生したときにバインドを解除しようとしましたが、onchange イベントが最初に実行されます。したがって、バインドが解除されることはありません。では、ユーザーが手動で入力したか、自動推測を介して入力したかを検出する可能性はありますか? ユーザーが手動で入力し、オートコンプリート ドロップダウンを使用しない場合にのみ、「requestlocation」機能を実行したいと考えています。「フォーカスアウト」などの他のイベントハンドラーで試しましたが、成功しませんでした。
この行はバインドを行います:
autoCompleteInput.on "change", requestlocation
これは呼び出される関数です:
requestlocation = () ->
address = autoCompleteInput.val()
geocoder = new google.maps.Geocoder()
geocoder.geocode
address: address, (results, status) ->
if status is google.maps.GeocoderStatus.OK
if results[0].address_components.length > 1
city = results[0].address_components[0].long_name
country = results[0].address_components[results[0].address_components.length-1].long_name
setHiddenFields results[0].geometry.location.lat(), results[0].geometry.location.lng(), city, country
autoCompleteInput.val(city+", "+country)
else
city = null
country = results[0].address_components[0].long_name
setHiddenFields results[0].geometry.location.lat(), results[0].geometry.location.lng(), city, country
autoCompleteInput.val(country)
setMarker new google.maps.LatLng(latInput.val(), lngInput.val())
else
console.log "Geocode was not successful for the following reason: " + status
これは、オートコンプリート ハンドラーが要求を行うコードです。
google.maps.event.addListener autocomplete, 'place_changed', ->
autoCompleteInput.off "change", requestlocation
place = autocomplete.getPlace()
if !!place.geometry
autoCompleteInput.attr "data-valid", "true"
setMarker place.geometry.location
address = place.address_components
if address.length > 1
setHiddenFields place.geometry.location.lat(), place.geometry.location.lng(), address[0].long_name, address[address.length-1].long_name
else
setHiddenFields place.geometry.location.lat(), place.geometry.location.lng(), null, address[0].long_name
else
autoCompleteInput.attr "data-valid", "false"
autoCompleteInput.on "change", requestlocation
回答ありがとうございます。