2014-10-01 54 views
1

那么,制作一个表单有多于1个地址的搜索框。 使用jquery google自动完成地址搜索。 与这个jQuery插件: ubilabs.github.io/geocompletejquery帮助需要 - 只把数据放在同一格下

试图让细节数据,这样不同的输入框: ubilabs.github.io/geocomplete/examples/form.html

$(".addressinputclass").geocomplete({ 
    details: ".thisisparentclass", 
    detailsAttribute: "data-geo" 
}); 

这是HTML:

  <div class="thisisparentclass"> 
       <input type="text" name="address1" class="addressinputclass" /> 
       <input type="text" name="writepostcodehere" placeholder="Post code" data-geo="postal_code" /> 
       <input type="text" name="writecountryhere" placeholder="Country Name" data-geo="country" /> 
      </div> 

,但现在的问题,当它是多个地址输入b牛,其他信息框获取最后的地址信息。 你可以在这里查看示例: http://jsfiddle.net/kosmspLm/

有帮助吗?


更新: 感谢来自Lesha Ogonkov &图沙·古普塔的帮助。 我得到了我的问题的解决方案。 :)

回答

1

Fiddle Demo

解决方案

$this.closest(".thisisparentclass"); 

查找与地址输入的thisisparentclass类当前父,使detailsAttribute只填充当前选择。

$(function() { 
    $(".addressinputclass").each(function() {//run each loop 
     var $this = $(this); //cache your selector, this refers to the current element 
     $this.geocomplete({ //attach auto-complete to the current element 
      details: $this.closest(".thisisparentclass"), //get the closest elements with class thisisparentclass 
      detailsAttribute: "data-geo" //attach data attribute 
     }); 
    }); 
}); 

问题

要附加到所有具有类thisisparentclass的元素,所以当你选择addess它填补所有的箱子和改变先前的选择。

$(".addressinputclass").geocomplete({ 
    details: ".thisisparentclass", //you are attaching to all the elements having this class 
    detailsAttribute: "data-geo" 
}); 
+1

哇!我尝试使用$(this).closest(“。thisisparentclass”),但它没有成功,没有考虑每个循环。非常感谢代码。 – crazymoin 2014-10-01 05:18:05

+0

@crazymoin很高兴帮助:) – 2014-10-01 05:21:47

0

你可以通过选择对插件

http://jsfiddle.net/kosmspLm/2/

$(function(){ 
    $('.addressinputclass[name="address1"]').geocomplete({ 
    details: $($(".thisisparentclass").get(0)), 
    detailsAttribute: "data-geo" 
    }); 
}); 

如果你需要一些领域的工作,我认为你需要循环的容器,就像这样:

$(function(){ 
    $(".thisisparentclass").each(function() { 
     var suggest, details; 

     details = $(this); 
     suggest = details.find('.addressinputclass'); 

     suggest.geocomplete({ 
     details: details, 
     detailsAttribute: "data-geo" 
     }); 
    }); 
}); 

http://jsfiddle.net/kosmspLm/3/