javascript - 如何获取 document.getElementById 的结果
问题描述
这是我的 Javascript
<script type="text/javascript">
window.onload=function(){
/* This showResult function is used as the callback function*/
function showResult(result) {
document.getElementById('latitude').value = result.geometry.location.lat();
document.getElementById('longitude').value = result.geometry.location.lng();
}
function getLatitudeLongitude(callback, address) {
// If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
address = address || 'Ferrol, Galicia, Spain';
// Initialize the Geocoder
geocoder = new google.maps.Geocoder();
if (geocoder) {
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0]);
}
});
}
}
var button = document.getElementById('btn');
button.addEventListener("click", function () {
var address = document.getElementById('address').value;
getLatitudeLongitude(showResult, address)
});
}
</script>
这是我的html代码
<input id="address" type="text" size="50" name="city" class="form-control" placeholder="Изберете локация" autocomplete="on">
<button id="btn" name="get" value="get">get</button>
<div>
The result of latidude and longitude id to be added as value
<p>Latitude:
<input type="text" id="latitude" name="latitude" readonly />
</p>
<p>Longitude:
<input type="text" id="longitude" name="longitude" readonly />
</p>
结果在输入时由 id 显示,但我无法使用变量
如何获取document.getElementById经纬度的结果并转换为php变量以用于mysql插入
解决方案
下面的 JavaScript 函数可能会帮助您将POST
请求发送到服务器上的文件以保存坐标值,save_coordinates.php
即保存在数据库中。latitude
longitude
function saveLatLong(latitude, longitude){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "save_coordinates.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("latitude="+latitude+"&longitude="+longitude);
return true;
}
推荐阅读
- java - 如何检查时间 SIMpleDateFormat?
- css - 在图像上制作完美的圆形
- javascript - 如何在 React 中为每个用户输入文本?
- c# - ActiveMQ 连接的消费者不消费消息
- reactjs - 页面刷新时丢失上下文
- python - 引发 AttributeError:响应内容不是文本 Scarpy 代理池。怎么解决?
- c++ - 如何下载和安装 Qt 库?
- api - 为什么不显示 NASA 图片?
- python - 如何在一个张量流概率层中混合多个分布?
- rest - 从 Unreal Engine 4.25 到 Postman Mock Server 的 VaREST POST