Javascript Google placeautocomplete第一个字母后的加载结果
我需要Google PlaceAutoComplete脚本的帮助。我花了将近58个小时在上面,但仍然不知道如何得到解决方案 如果我键入20 street并单击picklist结果,它将返回完整地址: 美国加利福尼亚州洛杉矶街20号。而我只想展示20街。不知怎的,我只找到了街道地址 但问题是,当我点击任何选择列表结果(下拉结果)时,它首先在文本框中返回完整的地址,然后我的脚本在文本框中返回我的地址。当用户单击下拉结果时,我试图停止在文本框中填充完整的地址,但未能找到解决方案 您可以在此处查看我的代码: 您可以在此处查看我的实时示例:Javascript Google placeautocomplete第一个字母后的加载结果,javascript,jquery,google-maps,google-maps-api-3,google-places-api,Javascript,Jquery,Google Maps,Google Maps Api 3,Google Places Api,我需要Google PlaceAutoComplete脚本的帮助。我花了将近58个小时在上面,但仍然不知道如何得到解决方案 如果我键入20 street并单击picklist结果,它将返回完整地址: 美国加利福尼亚州洛杉矶街20号。而我只想展示20街。不知怎的,我只找到了街道地址 但问题是,当我点击任何选择列表结果(下拉结果)时,它首先在文本框中返回完整的地址,然后我的脚本在文本框中返回我的地址。当用户单击下拉结果时,我试图停止在文本框中填充完整的地址,但未能找到解决方案 您可以在此处查看我的代
我已经试了一个星期了。任何帮助都将不胜感激 这当然不是最优雅的解决方案,但我一直在努力寻找一种通过API公开的方法来实现它 我使用了第二个输入字段(最初隐藏在“自动完成”输入字段后面),在从“自动完成”列表中选择一个位置后,我将该字段放在前面(因此它隐藏了真正的“自动完成”)。这可以防止用户看到文本框中填充的完整地址。请参见演示 HTML: Javascript:
/*
* Function called when the value in the autocomplete textbox changes so it hides the real autocomplete and displays the 'fake' one
*/
function hideAutocomplete() {
// Populate the fake autocomplete with the text typed by the user - this prevents a blank box appearing
document.getElementById('autocompleteDummy').value = document.getElementById('autocomplete').value;
// Display the fake autocomplete
document.getElementById('autocompleteDummy').style.zIndex = 1;
// Hide the real autcomplete
document.getElementById('autocomplete').style.zIndex = -1;
}
您还需要修改initAutocomplete()
函数,以便在填充地址字段后显示真正的自动完成。为此,请在方法结束之前和for循环之后添加以下行:
// Display the real autocomplete and hide the fake one
document.getElementById('autocomplete').style.zIndex = 1;
document.getElementById('autocompleteDummy').style.zIndex = -1;
我很想看看是否有人能想出一个更好的解决方案,因为这对我来说仍然有点“黑客”
/*
* Function called when the value in the autocomplete textbox changes so it hides the real autocomplete and displays the 'fake' one
*/
function hideAutocomplete() {
// Populate the fake autocomplete with the text typed by the user - this prevents a blank box appearing
document.getElementById('autocompleteDummy').value = document.getElementById('autocomplete').value;
// Display the fake autocomplete
document.getElementById('autocompleteDummy').style.zIndex = 1;
// Hide the real autcomplete
document.getElementById('autocomplete').style.zIndex = -1;
}
// Display the real autocomplete and hide the fake one
document.getElementById('autocomplete').style.zIndex = 1;
document.getElementById('autocompleteDummy').style.zIndex = -1;