我正在使用 Google 地址自动完成 API 使用 Google 在以下位置提供的标准示例代码来格式化内部应用程序的地址:
对于每个邮政编码,我正确地得到了类似于以下的响应:
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "BA1 1UN",
"short_name" : "BA1 1UN",
"types" : [ "postal_code" ]
},
{
"long_name" : "Bath",
"short_name" : "Bath",
"types" : [ "postal_town" ]
},
{
"long_name" : "Bath and North East Somerset",
"short_name" : "Bath and North East Somerset",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
除了“administrative_area_level_2”一个即县外,我所有的表单字段都很好。我的工作 HTML 如下所示:
<div class="form-group">
<label>Postal Town</label>
<input class="form-control field" placeholder="Enter your country"
required="true" name="postal_town" id="postal_town">
<p class="help-block">Enter your country.</p>
</div>
但这不会:
<div class="form-group">
<label>County</label>
<input class="form-control field" placeholder="Enter your county" required="true" name="administrative_area_level_2" id="administrative_area_level_2">
<p class="help-block">Enter your county.</p>
</div>
如何访问该字段以自动完成表单?
有时,走开并拥有新鲜的眼睛是很好的。很容易修复,标准的谷歌自动完成有以下代码:
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
postal_town: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
您只需要包含附加字段,代码就会显示:
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
postal_town: 'long_name',
administrative_area_level_1: 'short_name',
administrative_area_level_2: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
现在它会自动填充表单,我还没有尝试过,但假设它也适用于其他字段。爱谷歌!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句