2012-04-07 114 views
0

我試圖只允許Google地圖infowindow內的TextBox上的有效字符。 '按鍵'事件不是火。請幫我解決這個問題。TextBox按鍵事件不會在Google地圖infowindow中觸發

google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) { 

     var polylength=polygon.getPath().getArray().length; 
     var lastarray=polygon.getPath().getAt(polylength-1); 

      vertex=polygon.getPath().getArray(); 
     var contentString="<form id='myform'>Name:<input name='cname' id='cname' type='text' size='50'></input><br>Address<input name='addr' id='addr' type='text' size='50'></input><br><input type='button' value='submit' onclick='save();'></input></form>"; 
     infowindow = new google.maps.InfoWindow({ 
      content: contentString, 
      }); 
     infowindow.setPosition(lastarray); 
     infowindow.open(map); 
    }); 

$('input').bind('keypress', function(event){ 
       alert("pressed"); 
       var regex = new RegExp("^[a-zA-Z0-9 ]+$"); 
       var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); 
       if (!regex.test(key)) { 
        event.preventDefault(); 
        return false; 
       } 
     }); 

回答

2

當你bind荷蘭國際集團事件偵聽器input領域,input字段沒有創建yet.You需要添加infowindowdomready事件偵聽器,然後bindkeypress聽衆:

infowindow = new google.maps.InfoWindow({ 
    content: contentString, 
}); 
google.maps.event.addListener(infowindow , 'domready', function(){ 
    $('#myform input').bind('keypress', function(event){ 
      alert("pressed"); 
      var regex = new RegExp("^[a-zA-Z0-9 ]+$"); 
      var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); 
      if (!regex.test(key)) { 
       event.preventDefault(); 
       return false; 
      } 
    }); 
}); 
相關問題