当文本输入其中一个选项的相同值时,触发Google商家信息自动填充位置已更改

我正在使用Google Places Autcomplete API和jQuery。 它的基本function正常。 我想要做的是,如果有人在自动完成中输入了与我希望选择的项目和要触发的place_changed事件的选项之一完全相同的内容。 这是因为我在需要触发的地方改变了监听器中的一些逻辑。 这个想法是,如果有人输入与自动完成选项之一完全相同的文本,那么它应该像有人点击该选项一样。

这是我尝试过的,其中userLocation是输入:

 $("#userLocation").keyup(function(e){ //if the text is at least 4 chars long if($("#userLocation").val().length > 4) { //iterate through each of the autocomplete options $(".pac-item").each(function(){ if($(this).text() == $("#userLocation").val() ) { //does not do what I need it to $(this).trigger("place_changed"); } }); } }); 

我也尝试用.trigger("click")替换trigger("place_changed") ,但无济于事。

有没有人这样做过? 任何人都可以建议另一种方法来尝试使这项工作? 任何建议将非常感谢,谢谢!

正如答案中提到的:

您必须调用google.maps.event.trigger函数, google.maps.event.trigger产生如下调用。

 $("#userLocation").keyup(function(e){ //if the text is at least 4 chars long if($("#userLocation").val().length > 4) { //iterate through each of the autocomplete options $(".pac-item").each(function(){ if($(this).text() == $("#userLocation").val() ) { google.maps.event.trigger(autocomplete, 'place_changed'); } }); } }); 

文档显示您还可以将参数传递给trigger函数,该函数将传递给’place_changed’事件的事件侦听器。

您也可以尝试使用自动完成输入键事件并使用javascript触发。

请尝试以下代码。

 //For Set Location pin point var searchtext = "New York, NY"; var input = document.getElementById('pac-input'); $('#pac-input').val(searchtext); setTimeout(function () { google.maps.event.trigger(input, 'focus'); google.maps.event.trigger(input, 'keydown', { keyCode: 13 }); }, 10); //End