谷歌在克隆元素上映射自动完成



当有人点击添加更多按钮时,我得到了这个代码来克隆我的输入字段。

javascript

$(".multipleunitaddress").each(function(){
  var multipleunitaddressinput = $(this).get(0);
  autocomplete2 = new google.maps.places.Autocomplete(multipleunitaddressinput, 
  {
    componentRestrictions: {country: "nz"}
  });
});

$("#addmore").click(function(){
  var newelement = $("#multipleunitaddress").clone();
  $(this).before(newelement);
  autocomplete3 = new google.maps.places.Autocomplete(newelement,                                                      
  {
    componentRestrictions: {country: "nz"}
  });
})

html

<div class="controls" id="multipleunitaddresswrapper">
  <input type="text" id="multipleunitaddress" class="multipleunitaddress" name="multipleunitaddress" value="" class="required" placeholder="Property's Google-safe address" autocomplete="off">
  <button id="addmore" class="btn btn-danger">Add more</button>
</div>

默认输入运行谷歌地图自动完成罚款。添加更多按钮克隆了我的输入。然而,对于新的克隆输入,谷歌地图自动完成不适用。如何在克隆元素上使谷歌地图自动完成?

我使用livequery和以下代码使其工作

html

  <div class="controls">
    <div class="multipleunitaddresswrapper">
      <input type="text" class="multipleunitaddress" name="multipleunitaddress" value="" class="required" autocomplete="off">
    </div>
    <button id="addmore" class="btn btn-danger" type="button">Add more</button>
  </div>

javascript

$(".multipleunitaddress").livequery(function()
  {
    var multipleunitaddressinput = $(this).get(0);
    autocompleteInput = new google.maps.places.Autocomplete(multipleunitaddressinput, 
   {
    componentRestrictions: {country: "nz"}
  });
});
$("#addmore").click(function(){
  var newelement = $(this).prev(".multipleunitaddresswrapper").clone();
  newelement.find("input").val("");
  $(this).before(newelement);
})

相关内容

  • 没有找到相关文章

最新更新