Google Map API下拉菜单Onchange事件侦听器

Google Map API dropdown Onchange event Listener

本文关键字:事件 侦听器 Onchange 下拉菜单 Map API Google      更新时间:2023-09-26

我有一个谷歌地图,每当我从动态下拉列表中选择值时,我都想触发一个函数。为此,我添加了一个监听器,并提交了如下表格。

var input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('id', 'pac-input');
input.setAttribute('class', 'controls');
input.setAttribute('placeholder', 'Search By Area');
input.addEventListener("change", function(event) {
    checkInputKey_map(event, $(this));
});

checkInputKey中,我用所选的值向控制器发送ajax,但在请求后参数going不是所选的全名。相反,它是为了获得位置而编写的关键字。而我希望发送完整的搜索名称。如有任何帮助,将不胜感激

更新

function checkInputKey_map(event,this) {
    if ((event.keyCode == 13 || event.type == 'change') && $("#API_source_map").val() == 'yelp_places') {
        searched_place = $('#pac-input').val();
        console.log(searched_place);
    }
}

总是发布自己问题的答案,感觉很好,但很烦人。但所有的事情都是分开的。我做了一个技巧,将相同的监听器应用于输入,但延迟了一秒钟。我的最终代码如下

var input = document.createElement('input');
input.setAttribute('type','text');
input.setAttribute('id','pac-input');
input.setAttribute('class','controls');
input.setAttribute('placeholder','Search By Area');
input.addEventListener("change", function(event){
setTimeout(function() {
    checkInputKey_map(event, $(this));
},1000);
});