如何清除绑定到Google Places自动填充的输入? [英] How to clear the input bound to the Google Places Autocomplete?
问题描述
这是我的问题:我正在使用Google商家信息自动填充来收集用户有关地点的信息。
在事件place_changed上,我保存了这些信息。不过,我想给用户添加多个位置的可能性。所以在这个地方保存后,我想清除输入。
但Google自动填充会自动替换输入字段中的最后一个条目。无论如何摆脱这一点?
详细信息:
var inputDiv = $('#myinput');
var options = {
types:['(cities)']
};
var autocomplete = new google.maps.places.Autocomplete(inputDiv [0],options);
google.maps.event.addListener(autocomplete,'place_changed',function(){
var places = autocomplete.getPlace();
savePlaces(places);
console.log(inputDiv.val()); //'Paris,France'
inputDiv.val('');
console.log(inputDiv.val()) ; //(空字符串)
setTimeout(function(){
console.log(inputDiv.val()); //'Paris,France'(返回!)
inputDiv.val('');
console.log(inputDiv.val()); //(空字符串)
},1);
setTimeout(function(){
console.log(inputDiv.val()); //(一个空字符串)
},10);
}
$ c
$ b 你会告诉我,看起来很好,只是在超时时清除它
但是当我离开输入(它失去了焦点)最后一个条目又回来了!
有什么想法解决这个问题?
我在Google文档中找不到一个函数SUC h
autocomplete.clear();
谢谢!
解决方案看起来,自动完成在内部侦听输入的模糊事件,所以让我们给它一些东西来监听和清除之后的字段:
inputId_div.blur();
setTimeout(function(){
inputId_div.val('')
inputId_div.focus();},10);
Here is my issue: I am using Google Places Autocomplete to gather information about places from the user.
On the event "place_changed", I save this information. However I want to give the user the possibility to add multiple places. So after this place has been save I want to clear the input.
However Google Autocomplete automatically replace the last entry in the input field. Anyway to get rid of that?
The details :
var inputDiv = $('#myinput');
var options = {
types: ['(cities)']
};
var autocomplete = new google.maps.places.Autocomplete(inputDiv[0], options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var places = autocomplete.getPlace();
savePlaces(places);
console.log(inputDiv.val()); // 'Paris, France'
inputDiv.val('');
console.log(inputDiv.val()); // (an empty string)
setTimeout(function() {
console.log(inputDiv.val()); // 'Paris, France' (It's back!)
inputDiv.val('');
console.log(inputDiv.val()); // (an empty string)
}, 1);
setTimeout(function() {
console.log(inputDiv.val()); // (an empty string)
}, 10);
}
You will tell me, well that looks fine, just clear it in the timeout.
BUT when I click away from the input (it loses the focus). The last entry comes back again!
Any idea to fix that?
I haven't found in the Google documentation a function such as
autocomplete.clear();
Thanks!
解决方案 It appears that the autocomplete internally listens to the blur-event of the input, so lets give it something to listen for and clear the field after that:
inputId_div.blur();
setTimeout(function(){
inputId_div.val('')
inputId_div.focus();},10);
这篇关于如何清除绑定到Google Places自动填充的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!