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

The*_*hod 4 jquery google-api google-places-api

我正在使用Google Places Autcomplete API和jQuery.它的基本功能正常.我想要做的是,如果有人在自动填充中输入了与我希望选择的项目和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");
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

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

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

Chr*_*een 5

正如答复中提到在这里:

您必须调用该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');
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

文件显示,您还可以将参数传递给trigger将要传递给事件侦听器"place_changed"事件的功能.