2010-06-14 72 views
1

爲什麼焦點未設置爲noteTitle?無法在infoWindow中的輸入字段上設置焦點

我正在使用谷歌地圖API V3。

getNoteForm()返回一個輸入字段「noteTitle」。

$(「#noteTitle」)。focus()在firebug中執行時工作正常。

我在地圖上點擊時調用這個函數:

function setNewNoteInfowindow(latlng) { 
if (geocoder) { 
     geocoder.geocode({'latLng': latlng}, function(results, status) { 
    var address = ""; 

    if (status == google.maps.GeocoderStatus.OK) { 
    if (results[1]) { 
    address = results[1].formatted_address; 
      } 
     } 

    newNoteInfowindow = new google.maps.InfoWindow({ 
    content: getNoteForm(latlng, address), 
     size: new google.maps.Size(40,50) 
    }); 

    newNoteInfowindow.open(map, newNoteMarker); 

    google.maps.event.addListener(newNoteInfowindow, 'domready', function() { 
      $("#noteTitle").focus(); 
     }); 

    google.maps.event.addListener(newNoteInfowindow, 'closeclick', function() { 
      newNoteMarker.setVisible(false); 
     }); 
    }); 
} 
} 
+0

您是否嘗試過移動'$(「#noteTitle」)。focus();'語句在別處?也許從$(document).ready開始吧? – HurnsMobile 2010-06-14 16:31:48

+0

是的,但運氣好的話...... – thomas 2010-06-14 16:39:22

+0

您是否驗證了該事件是否正確啓動?添加一個alert('something');'看看它是否執行.. – 2010-06-14 17:27:08

回答

1

它看起來像您要添加的事件監聽器之前打開您的信息窗口。所以當你添加監聽器時,事件已經被解僱了。嘗試顛倒open()和addListener()的順序

google.maps.event.addListener(newNoteInfowindow, 'domready', function() { 
      $("#noteTitle").focus(); 
     }); 

    newNoteInfowindow.open(map, newNoteMarker); 
+0

是的,我也嘗試過......沒有運氣。 – thomas 2010-06-15 07:44:21

+2

像這樣解決了。不漂亮,但它的作品。 (函數(事件){ \t placeNewNote(event.latLng); \t setTimeout(function(){$(「#noteTitle」)。focus()}, 500); }); – thomas 2010-06-15 07:45:40

相關問題