将Google Places自动完成功能添加到列表项中新生成的输入中

爪哇

var start = new google.maps.places.Autocomplete(
    (document.getElementById('start')), {
      types: ['geocode']
    });
<input id="start" placeholder="Where to begin?" type="text" />

<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=places"></script>

我可以在文本字段中使Google地方信息自动完成。

我想要一个按钮来生成这些字段作为li并在每个字段中包括自动完成功能。这是一个没有自动完成功能的示例:

document.getElementById('addplace').addEventListener('click', function() {
    addplace();
  });

var j=1;
function addplace() {
  var node = document.createElement("li"); 
  node.innerHTML = "<input id='place" + j +"' placeholder='Pit stop " + j + "' onFocus='geolocate()' type='text' />";               
  document.getElementById("waypoints").appendChild(node);
  j++; 
}
<input type="submit" id="addplace" value="Add One!">
<ul id="waypoints"></ul>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=places"></script>

因此,生成的输入的ID为place1,place2等。

我尝试添加

var place"+j+" = new google.maps.places.Autocomplete(
(document.getElementById('place"+j+"')), {
  types: ['geocode']
});

到addplace函数的定义中,但它似乎导致我所有的javascript被禁用(该地图不再显示)。不知道为什么;我不认为Google会阻止它。也许我犯了语法错误,或者放置“ + j +”不是命名变量的正确方法?

感谢您的帮助,将自动完成地点自动添加到每个生成的输入字段。

更新:我注意到这个问题,Google Places Autocomplete在动态生成的输入元素上不起作用,将对其进行研究以在此处尝试应用,但是我注意到使用了ajax以及其他可能比该示例更复杂的东西。也就是说,他们在答案中引用了http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js如果我不用jquery就可以做到这一点,那对我来说可能会更好,或者更直接或更具有教育意义,因为我认为我的部分解决方案至少很接近/通过一些调整可能是正确的吗?

divy3993

我想这就是您要寻找的东西:

1)。添加具有特定按钮的按钮id然后,使用任何元素(此处ul带有id = "waypoints"包装所有自动完成输入。

2)。然后添加click event到按钮,该按钮会生成带有dynamic的“自动完成输入” id's

3)。现在,为具有(here 的元素(here ul所有这些(如果有的话)创建的输入创建click事件,同时针对单击的Autcomplete Input创建autcomplete实例id id = "waypoints"

片段:

var totalAC = 0; 
$(document).on('click', "#waypoints input[type=text]",function () {
	var currentInp = $(this).attr("id");
	var placeBox = new google.maps.places.Autocomplete(document.getElementById(currentInp));
});

$("#newAutocomplete").click(function(){
	totalAC = $("#waypoints input").length;
	totalAC = totalAC + 1;
	var codeVar = "<li><input id='place" + totalAC + "' placeholder='Where to begin?' type='text' /></li>";
	$("#waypoints").append(codeVar);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=places"></script>

<button id="newAutocomplete">Add One+</button>
<ul id="waypoints">

</ul>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将Google Places自动完成功能添加到flask?

如何将预览文本添加到自定义的自动完成功能中?

将类添加到生成的列表中的列表项

从命令行自动将新生成的组件添加到 entryComponents 中

将类添加到jQuery中的活动列表项

Angular 2 + Google Maps Places自动完成功能,在输入后追加[Dom操作]

如何将列表项添加到在后面的代码中生成的下拉列表中?

将代码完成功能添加到我在Komodo Edit中的所有文件中

iOS上的Google Places API自动完成功能

无法删除Google Places自动完成功能?

将 Google 地方自动完成功能限制在城市内

google.maps.places。带有JHipster中的模式的自动完成功能

Google Places自动完成功能在我的模拟器或VMware中不起作用

如何在ExtJS中从google.maps.places.autocomplete创建自动完成功能

将代码添加到SWIG中自动生成的类

Google将JavaScript自动完成功能的API放置在建议中

将功能添加到Java列表中

Google表格/应用脚本可在输入值时自动将日期戳添加到表格中

如何自动将字符 _ 添加到用户输入的字符串或列表中,使 2 个列表的长度相同

将输入存储在字典中以添加到主列表

在Google脚本中创建与Google表格中的值列表配合使用的自动完成功能

将列表项添加到无序列表

Google Scripts 自动将日期添加到 Google 表格中的列

如何将列表项添加到现有的无序列表中?

如何将列表项添加到元组列表中的元组

将重定向URI添加到自动生成的Google OAuth 2.0客户端ID

将事件自动添加到Google日历

将输入添加到列表

Google Place自动完成功能会影响输入中的按键按下行为