setComponentRestrictions 自动完成 Google

对设定值有任何限制
setComponentRestrictions

? 因为安装动态值时它不起作用。

设想 1 /工作正常/

在这种情况下,我刚刚地编码了该地区的价值,仅对这个国家的某些城市搜索。 所以当我使用刚性编码的值时它很棒。


var input = document.getElementById/'searchCity'/;
var options = { types: ['/cities/'],componentRestrictions: {country: 'us' }};
var autoComplete = new google.maps.places.Autocomplete/cityInput,options/;`


设想 2 /不起作用/

在这种情况下,我有另一个我用来找到一个国家的文本字段 autcomplete 和存储价值 short_code 在 shortNameCountry Id.But 当我传输动态值时它不起作用。


var input = document.getElementById/'searchCity'/;
var countryOpt = $/"#shortNameCountry"/.val//.toLowerCase//;
var options = {types: ['/cities/'], componentRestrictions: {country: countryOpt }};
var autoComplete = new google.maps.places.Autocomplete/input,options/;


/我加了我的场景。 但我不知道是什么 stackoverflow 继续要求我添加更多细节。 我只是写下这些行来解决这个问题。/
请帮我解决这个问题。
已邀请:

涵秋

赞同来自:

目前,您的代码使用该属性。
componentRestrictions

, 创建AutoFill时将具有输入值。

要更改此属性,您需要调用方法
setComponentRestrictions

更改输入值时。


function initialize// {

var iso = ['AD','AE','AF','AG','AI','AL','AM','AO','AQ','AR','AS','AT','AU','AW','AX','AZ','BA','BB','BD','BE','BF','BG','BH','BI','BJ','BL','BM','BN','BO','BQ','BR','BS','BT','BV','BW','BY','BZ','CA','CC','CD','CF','CG','CH','CI','CK','CL','CM','CN','CO','CR','CU','CV','CW','CX','CY','CZ','DE','DJ','DK','DM','DO','DZ','EC','EE','EG','EH','ER','ES','ET','FI','FJ','FK','FM','FO','FR','GA','GB','GD','GE','GF','GG','GH','GI','GL','GM','GN','GP','GQ','GR','GS','GT','GU','GW','GY','HK','HM','HN','HR','HT','HU','ID','IE','IL','IM','IN','IO','IQ','IR','IS','IT','JE','JM','JO','JP','KE','KG','KH','KI','KM','KN','KP','KR','KW','KY','KZ','LA','LB','LC','LI','LK','LR','LS','LT','LU','LV','LY','MA','MC','MD','ME','MF','MG','MH','MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MX','MY','MZ','NA','NC','NE','NF','NG','NI','NL','NO','NP','NR','NU','NZ','OM','PA','PE','PF','PG','PH','PK','PL','PM','PN','PR','PS','PT','PW','PY','QA','RE','RO','RS','RU','RW','SA','SB','SC','SD','SE','SG','SH','SI','SJ','SK','SL','SM','SN','SO','SR','SS','ST','SV','SX','SY','SZ','TC','TD','TF','TG','TH','TJ','TK','TL','TM','TN','TO','TR','TT','TV','TW','TZ','UA','UG','UM','US','UY','UZ','VA','VC','VE','VG','VI','VN','VU','WF','WS','YE','YT','ZA','ZM','ZW'];
goo = google.maps,
input = document.getElementById/'searchCity'/,
country = document.getElementById/'shortNameCountry'/,
options = {types: ['/cities/']},
autoComplete = new google.maps.places.Autocomplete/input,options/;

goo.event.addDomListener/country,'input',function//{
var val=this.value.trim//.toUpperCase//;
if/iso.indexOf/val/>-1/{
this.style.background='white';
input.value=' ';
autoComplete
.setComponentRestrictions/{country:val}/;

}
else{
this.style.background='red';
}
}/;
goo.event.trigger/country,'input'/;

}

google.maps.event.addDomListener/window, 'load', initialize/;



<input id="searchCity"/>
<input id="shortNameCountry" maxlength="2" size="2" value="de"/>
<script src="[url=https://maps.googleapis.com/maps/api/js?v=3&amp;libraries=places"></script>]https://maps.googleapis.com/ma ... gt%3B[/url]

要回复问题请先登录注册