Javascript 如何获取document.getElementById的结果
这是我的JavascriptJavascript 如何获取document.getElementById的结果,javascript,php,jquery,Javascript,Php,Jquery,这是我的Javascript <script type="text/javascript"> window.onload=function(){ /* This showResult function is used as the callback function*/ function showResult(result) { docu
<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>
window.onload=function(){
/*此showResult函数用作回调函数*/
函数showResult(结果){
document.getElementById('latitude')。value=result.geometry.location.lat();
document.getElementById('longitude')。value=result.geometry.location.lng();
}
函数getLatitudeLongitude(回调,地址){
//如果未提供地址,请使用默认值“Ferrol,Galicia,西班牙”
地址=地址| |‘西班牙加利西亚费罗尔’;
//初始化地理编码器
geocoder=新的google.maps.geocoder();
if(地理编码器){
地理编码({
“地址”:地址
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
回调(结果[0]);
}
});
}
}
var button=document.getElementById('btn');
按钮。addEventListener(“单击”),函数(){
var address=document.getElementById('address')。值;
GetLatitudeLongTude(显示结果、地址)
});
}
这是我的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的结果
纬度:
经度:
结果在输入时按id显示,但我无法使用变量获取
如何获得document.getElementById纬度和经度的结果,并将其转换为php变量以用于mysql插入下面的JavaScript函数可能会帮助您向服务器上的
保存坐标.php
文件发送POST
请求以保存坐标值,即,数据库中的纬度和经度
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;
}
您不能将JS中的某些东西转换成PHP变量,必须将其传递给服务器,卢卡是正确的,您必须考虑页面重新加载或Ajax调用。使用$(文档)。