javascript Google 自动完成 HTMLInputElement 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34337196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:34:51  来源:igfitidea点击:

Google Autocomplete HTMLInputElement error

javascriptgoogle-api

提问by ottz0

I am using Google Autocomplete and am getting this error

我正在使用 Google 自动完成并收到此错误

InvalidValueError: not an instance of HTMLInputElement

I think Google has getElementById for deprication

我认为谷歌有 getElementById 用于降价

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

However i'm unsure on it's usage. My code below

但是我不确定它的用法。我的代码如下

var options ={
    types:['(cities)'],
};

var input = document.getElementById('destination');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}

google.maps.event.addDomListener(window, 'load', initialize);

回答by Guneet Singh

Use onFocus="functionName()"which calls google.maps.autocompleteon input tag.

在输入标签上使用onFocus="functionName()"which 调用google.maps.autocomplete

回答by Aleksey Spirin

check if the your function is in $(document).ready function or your document.getElementById won't be found by js (cos it tries to find it before it appears in document)

检查您的函数是否在 $(document).ready 函数中,或者您的 document.getElementById 不会被 js 找到(因为它试图在它出现在文档中之前找到它)

回答by user2718671

It's because you overwrite your input variable. Should work if you just use var input = /** @type {!HTMLInputElement} */( document.getElementById('pac-input')); or replace "pac-input" with "destination"

这是因为您覆盖了输入变量。如果你只使用 var input = /** @type {!HTMLInputElement} */( document.getElementById('pac-input')); 或将“pac-input”替换为“destination”