我尝试使用输入按钮中的 maxlength 来显示谷歌地图的纬度和经度,但 maxlenght 不起作用...我做错了什么?
var marker = new google.maps.Marker({
draggable: true,
position: {lat: -47.00, lng: 18.00},
map: map,
});
google.maps.event.addListener(marker, 'dragend', function (event) {
document.getElementById("lat").value = event.latLng.lat();
document.getElementById("long").value = event.latLng.lng();
});
HTML:
Latitu: <input id="lat" name="lat" maxlength="6" value=""> <br />
Longi: <input id="long" name="long" maxlength="6" value=""/>
最佳答案
来自specification :
declares a limit on the number of characters a user can input
当您通过 JS 设置值时,它不是用户输入的。
您需要通过 JS 限制字符数:
document.getElementById("lat").value = String(event.latLng.lat()).substr(0,6);
关于javascript - 谷歌地图中纬度和经度的最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34044329/