[英]how to get google places autocomplete to work if the textbox is initially hidden and should be shown only after a postback
我有兩個需要使用Google Places自動完成功能的文本框。 這些文本框包含在面板中,該面板在頁面加載時隱藏。 有可供選擇的選項列表,一旦獲得用戶輸入,就應該顯示隱藏的面板。 我都嘗試過
Panel.visible = false;
和
Panel.Style["display"] = "none";
Panel.Style["visibility"] = "hidden";
但是都沒有用。 隱藏面板后,文本框的自動完成功能將停止工作。 我最初無法顯示面板。 有沒有解決的辦法? 特定回發后可以觸發自動完成嗎? 還是其他方式? 這是我用於自動完成的JavaScript
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<script type="text/javascript">
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(7.623887, 68.994141),
new google.maps.LatLng(37.020098, 97.470703));
var input1 = document.getElementById('ctl00_ReportContentPlaceHolder_txtLocality1');
var input2 = document.getElementById('ctl00_ReportContentPlaceHolder_txtLocality2');
var options = {
bounds: defaultBounds,
types: ['geocode'],
componentRestrictions: { country: "IN" }
};
autocomplete1 = new google.maps.places.Autocomplete(input1, options);
autocomplete2 = new google.maps.places.Autocomplete(input2, options);
</script>
我終於解決了我的問題...在這里找到答案: https : //stackoverflow.com/a/8851767/972821
我有點意識到,回發后我不得不重新初始化javascript ...但不確定如何完成...謝謝Aristos。 這是我修改的代碼:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args) {
}
// fires after the partial update of UpdatePanel
function EndRequest(sender, args) {
initialize();
}
function initialize() {
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(7.623887, 68.994141),
new google.maps.LatLng(37.020098, 97.470703));
var input1 = document.getElementById('ctl00_ReportContentPlaceHolder_txtLocality');
var input2 = document.getElementById('ctl00_ReportContentPlaceHolder_txtDropLocality');
var options = {
bounds: defaultBounds,
types: ['geocode'],
componentRestrictions: { country: "IN" }
};
autocomplete1 = new google.maps.places.Autocomplete(input1, options);
autocomplete2 = new google.maps.places.Autocomplete(input2, options);
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.