Javascript 如果输入字段中至少有n个字母,如何将Google Places Autocomplete配置为仅执行请求?

Javascript 如果输入字段中至少有n个字母,如何将Google Places Autocomplete配置为仅执行请求?,javascript,google-maps,input,google-places-api,Javascript,Google Maps,Input,Google Places Api,我的代码: var input = /** @type {!HTMLInputElement} */( document.getElementById('place')); var options = { types: ['(regions)'], componentRestrictions: {country: "de"}, offset: 3 };

我的代码:

var input = /** @type {!HTMLInputElement} */(
            document.getElementById('place'));              

var options = {               
        types: ['(regions)'],
        componentRestrictions: {country: "de"},
        offset: 3
    };  
function initialize() {             
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
       //some stuff here
    });
}


google.maps.event.addDomListener(window, 'load', initialize);
来自谷歌的文本:

可选参数

偏移量-输入术语中>服务用于匹配预测的最后一个字符的位置。例如,如果输入 是“谷歌”,偏移量是3,服务将匹配“谷歌”。这个 由偏移量确定的字符串与中的第一个字匹配 仅输入术语。例如,如果输入术语为“Google abc” 如果偏移量为3,则服务将尝试与“Goo”匹配 abc’。如果没有提供补偿,服务将使用整个期限。 偏移量通常应设置为文本插入符号的位置


那么,为什么“偏移”选项不起作用呢?我在收到第一封信后已经收到了建议。但是我想减少请求。

您可以使用jquery onchange事件侦听器,例如:

$('#yourinput').on('change', function(e){
    var n = 3
    if($(this).value().length() < n){
        return
    }else{
        //do your autocomplete
    }
$('#yourinput')。关于('change',函数(e){
变量n=3
if($(this).value().length()

其中
var n
是您希望用作地理编码开始的字符数。

如果我的答案解决了您的问题,请将答案标记为正确,以便将来的读者知道这是可行的