在提交前输入lat和lng值到表单隐藏字段
Google Maps API Infowindow Inputing lat and lng Values into Form Hidden Field before Submit
我试图将'lat'和'lng'值输入到嵌入在信息窗口内的表单隐藏字段中。在提交表单时,表单将调用一个函数,该函数在提交表单之前将'lat'和'lng'值附加到隐藏字段中。
然而,当试图将'lat'和'lng'的值传递到隐藏字段时,我面临问题。
//declare global variables
var map = null;
var info_window = new google.maps.InfoWindow();
// handles the initial settings and styling for google maps
function initialize() {
var map_options = {
zoom: 11,
center: new google.maps.LatLng(1.35208, 103.81984),
panControl: false,
panControlOptions: {
position: google.maps.ControlPosition.BOTTOM_LEFT
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.RIGHT_CENTER
},
scaleControl: false
}
map = new google.maps.Map(document.getElementById('map-canvas'), map_options);
//add marker to the event where the point of the map is clicked
google.maps.event.addListener(map, 'click', function(event) { add_marker(event.latLng); });
}
function add_marker(location) {
map.markers = [];
//ensure that the map is initialized
if(map != null) {
var marker = new google.maps.Marker({
position: location,
map: map,
animation: google.maps.Animation.DROP });
//listener on markers to listen for clicks/right clicks
google.maps.event.addListener(marker, 'rightclick', function(event) { marker.setMap(null); });
google.maps.event.addListener(marker, 'click', function() { open_info(marker, location) });
map.markers.push(marker);
}
}
function open_info(marker, location) {
if(map != null) {
//infowindow form that calls appendLatLng() before submit
var content = '<form method="post" action="main.php" onsubmit="return appendLatLng(location)" name="infoWindow" id="infoWindow">' +
'<input type="text" name="location" id="location" placeholder= "Location"/><br />' +
'<input type="textarea" name="description" id="description" cols="40" rows="5" placeholder="Description"/><hr />' +
'<input type="hidden" name="lat" id="lat" value=""/>' +
'<input type="hidden" name="lng" id="lng" value=""/>' +
'<input type="submit" name="save" id="save" value="Save" />' +
'</form>';
info_window.setContent(content);
info_window.open(map, marker);
}
}
//append location.lat() and location.lng() to lat and lng fields
function appendLatLng(location){
document.getElementById('lat').value = location.lat();
document.getElementById('lng').value = location.lng();
}
google.maps.event.addDomListener(window, 'load', initialize);
以下是main.php中的php代码段,试图访问lat和lng值,但我得到空值。
<?php
if (!empty($_POST['location']) && !empty($_POST['description'])){
?>
<h5><b>Name of Location:</b> <?=$_POST['location']?>
<b>Description:</b> <?=$_POST['description']?>
<b>Longtitude:</b> <?=$_POST['lng']?>
<b>Latitude:</b> <?=$_POST['lat']?></h5>
<?php
}
?>
我想知道如果它是我传递"位置"对象的问题,因为我调用appendLatLng()函数。有人能帮我一下吗?
谢谢!:)
onsubmit-handler不知道location
-变量,它将不会在open_info()
的范围内执行(在onsubmit-handler中location
将是一个DOMNode,输入ID为"location")
在准备表单时直接设置所需的值:
var content = '<form method="post" action="main.php" name="infoWindow" id="infoWindow">' +
'<input type="text" name="location" id="location" placeholder= "Location"/><br />' +
'<input type="textarea" name="description" id="description" cols="40" rows="5" placeholder="Description"/><hr />' +
'<input type="hidden" name="lat" id="lat" value="'+marker.getPosition().lat()+'"/>' +
'<input type="hidden" name="lng" id="lng" value="'+marker.getPosition().lng()+'"/>' +
'<input type="submit" name="save" id="save" value="Save" />' +
'</form>';
在info_window.open(map, marker);
之后插入appendLatLng(location);
。出于测试目的,将输入id lat和lng设置为文本,当您单击标记时,您可以看到添加的值。
相关文章:
- 使用javascript取消隐藏和隐藏表单中的某些元素
- 提交时隐藏RSForm表单
- 添加和删除隐藏字段数组中的值,而不提交表单
- HTML5在提交并显示所需标签后显示隐藏的表单元素
- 我的表单上的SpringWebFlow2中的每个转换都需要隐藏按钮吗
- 在提交时设置表单隐藏值
- 用第一个表单值(HTML、Javascript)填充第二个表单隐藏字段
- Javascript表单隐藏可见操作
- 可以'无法将表单隐藏在JS中
- 获取函数的表单隐藏变量
- jquery ui模式框中的表单隐藏后,键盘将不再被调用
- 获取从函数到 php 的表单隐藏变量
- 将表单隐藏字段变量从一个函数传递到另一个函数
- 在iFrame(不同域)表单隐藏字段中抓取父级引用
- 表单隐藏值不工作
- 从复选框中获取值-将其发送到表单(隐藏字段)
- HTML表单隐藏变量不显示在php
- 在提交前输入lat和lng值到表单隐藏字段
- 使用按钮 ID 更新表单隐藏字段值
- HTML表单隐藏/显示基于Div