Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在调用另一个eventhandler时禁用该eventhandler_Javascript_Jquery_Google Maps_Coffeescript - Fatal编程技术网

Javascript 如何在调用另一个eventhandler时禁用该eventhandler

Javascript 如何在调用另一个eventhandler时禁用该eventhandler,javascript,jquery,google-maps,coffeescript,Javascript,Jquery,Google Maps,Coffeescript,我在一个输入字段上有两个事件侦听器。当你点击谷歌的“自动完成”下拉列表时,其中一个会在更改时被调用,另一个会被调用。我的问题是,当你点击这样一个自动完成时,两个处理程序都会被调用,并向GoogleAPI发出请求 我试图绑定onchange事件,并在触发googleautocompleteajax调用时解除绑定,但onchange事件是首先执行的。所以它不会被解除绑定。 那么,是否有可能检测到用户是手动输入还是通过自动增强输入?我只想在用户手动输入并且不使用自动完成下拉列表时执行“requestl

我在一个输入字段上有两个事件侦听器。当你点击谷歌的“自动完成”下拉列表时,其中一个会在更改时被调用,另一个会被调用。我的问题是,当你点击这样一个自动完成时,两个处理程序都会被调用,并向GoogleAPI发出请求

我试图绑定onchange事件,并在触发googleautocompleteajax调用时解除绑定,但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
谢谢你的回答

我不知道“requestlocation”是什么样子。但是检查变更事件的不同事件属性如何。(event.target/event.currentTarget)我现在无法测试它,但您可能会发现其中存在差异。只是个主意

function requestlocation(event) {
  console.log(event.target, event.currentTarget);
}

var timeout;
autoCompleteInput.on("input", function() {
  clearTimeout(timeout);
  timeout = setTimeout(requestlocation, 100);
});

很抱歉使用普通的javascript,但我不喜欢coffeescript:///p>第一种方法:听
keyup
事件,而不是
change
,确定处理程序中按键的类型(过滤掉箭头键、tab键和返回键),只有在发生适当的更改时才调用
requestlocation
。可能不会真正起作用,因为文本可以粘贴到不会触发任何键事件的输入字段中

下一次尝试:使用一个小的超时,并在自动完成事件触发时停止它

autocomplete.on "change", ->
  # stop previous timeouts
  clearTimeout autocomplete.data('timeout')
  # start a new timeout and store reference
  timeout = setTimeout requestlocation, 100
  autocomplete.data timeout: timeout
在自动完成处理程序中,您应该终止运行超时并执行调用:

google.maps.event.addListener autocomplete, 'place_changed', ->
  # stop any running timeout
  clearTimeout autocomplete.data('timeout')
  # perform your autocompletion calls
  place = autocomplete.getPlace()
  ...

这还可以防止在快速触发多个
change
事件时进行多个调用。您可能需要将超时(此处:100ms)调整为合适的值。

不幸的是,在requestlocation中,target和currentTarget之间没有区别。但是,我认识到自动完成事件侦听器没有事件对象。。。但这也帮不了我。切换到“输入”事件怎么样?不确定当输入通过JS更改时是否有效(这只在现代浏览器中有效,所以我不知道这是否是一个选项),只是做了一个小测试。。。至少在Chrome中,“输入”事件仅由手动输入触发。。。但是如果另一个事件侦听器更改了内容,则输入侦听器不会触发。。。(作为旁注:在不支持输入事件的浏览器中,您也可以使用“keyup”事件)这将起作用,但问题是我将结果从requestlocation插入到输入字段。侦听器“输入”从第一个书面字母发出请求,并用第一个结果替换输入中的文本。我需要的东西,直到用户完成写作。好的,有点棘手。。。你可以使用节流机制