我正在使用 HTML5 地理定位,并且希望将纬度和经度存储到两个单独的文本框中,而不是简单地显示在屏幕上
下面是代码:
Current Location: <BR>
<button onclick="getLocation()">Locate</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
最佳答案
假设您要设置值的两个文本框是:
Latitude: <input type="text" id="lat"><br>
Longitude: <input type="text" id="long">
您可以在 JS 中添加以下行来选择适当的文本框并存储值。
document.getElementById("lat").setAttribute("value", position.coords.latitude);
关于javascript - 将 javascript 变量存储到 HTML 文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444679/