我尝试在Google地图信息窗口内的TextBox上只允许有效字符。 'keypress'事件不是火灾。请帮我解决这个问题。
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;
}
});
答案 0 :(得分:2)
当bind
事件监听器input
字段时,input
字段尚未创建。您需要在infowindow
上添加domready事件监听器},然后bind
keypress
听众:
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;
}
});
});