问题描述
我在使用 Google Autocomplete 访问最后一个搜索结果时遇到问题。
预期结果
带有最新搜索结果和任何给定参数(例如 name
)的警报,例如当点击按钮时。
实际结果
我使用了来自 Google 的 Place Autocomplete 页面的给定脚本,添加了一个按钮和此代码:
console.log(place.name);
$("#lb").click(function() {
alert(place.name);
});
JSfiddle
请检查我在 JSfiddle 的代码:https://jsfiddle.net/vloryan/1z2oscrj/
解决方案
我在此期间搜索并尝试了 48 小时,但没有成功。我将非常感谢对此的解决方案。非常感谢!
解决方法
这里是: https://jsfiddle.net/nLuzahpj/1/
我首先创建一个变量,然后在函数 initMap() 中将 place.name 分配给该变量。 而你在这个函数之外执行警报。
var hey; //Outside the function,at the beggining of your code
hey = place.name; //In your function
$("#lb").click(function() { //Next to your function
alert(hey);
});