javascript - 将显示在HTML 表单中的结果传递给另一个页面
问题描述
我有一个 JavaScript,当我单击地图时显示 Long 和 lat,一切正常,我可以<span id="onIdlePositionView"></span>
使用 id 值在我的 HTML 中显示这个跨度内的 JavaScript 结果。
现在,我想要将该值传递到 HTML 表单并将其发送到另一个页面。
这是代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
<script type="text/javascript"src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDVIHo_tvbLXtuzQJVYRolxIi0_Lk_8nFs"></script>
<script src="https://unpkg.com/location-picker/dist/location-picker.min.js"></script>
<style type="text/css">
#map {
width: 100%;
height: 480px;
}
</style>
</head>
<body>
<div id="map"></div>
<br>
<button id="confirmPosition" type="hidden"></button>
<br>
<!-- DISPLAY LONG AND LAT FROM THE MAP -->
<span id="onIdlePositionView"></span>
<!-- FORM TO SEND VALUE OF SPAN TO THE SECOND PAGE -->
<form action="location_picker_map.php" method="post">
<input type="submit" name="submito">
</form>
<!-- SCRIPT TO FIND LONG AND LAT AND DSIPAY INTO THE SPAN VALUE -->
<script>
// Get element references
var confirmBtn = document.getElementById('confirmPosition');
var onClickPositionView = document.getElementById('onClickPositionView');
var onIdlePositionView = document.getElementById('onIdlePositionView');
// Initialize locationPicker plugin
var lp = new locationPicker('map', {
setCurrentPosition: true, // You can omit this, defaults to true
}, {
zoom: 15 // You can set any google map options here, zoom defaults to 15
});
// Listen to button onclick event
confirmBtn.onclick = function () {
// Get current location and show it in HTML
var location = lp.getMarkerPosition();
onClickPositionView.innerHTML = '' + location.lat + ',' + location.lng;
};
// Listen to map idle event, listening to idle event more accurate than listening to ondrag event
google.maps.event.addListener(lp.map, 'idle', function (event) {
// Get current location and show it in HTML
var location = lp.getMarkerPosition();
onIdlePositionView.innerHTML = '' + location.lat + ',' + location.lng;
});
</script>
</body>
</html>
解决方案
所以我在尝试不同的选项后解决了这个问题,我所做的是而不是 .innerHTML
我使用.value
并将文本输入字段的 id 设置为变量
以下是更新后的代码:
<!-- FORM TO SEND VALUE OF SPAN TO THE SECOND PAGE -->
<form action="location_picker_map.php" method="post">
<button id="confirmPosition" type="hidden"></button>
<input type="text" name="fd_onIdlePositionView" id="id1" >
<input type="submit" name="submito">
</form>
<!-- SCRIPT TO FIND LONG AND LAT AND DSIPAY INTO THE SPAN VALUE -->
<script>
// Get element references
var confirmBtn = document.getElementById('confirmPosition');
var onClickPositionView = document.getElementById('onClickPositionView');
var onIdlePositionView = document.getElementById('id1');
// var inputF = document.getElementById("id1");
// Initialize locationPicker plugin
var lp = new locationPicker('map', {
setCurrentPosition: true, // You can omit this, defaults to true
}, {
zoom: 15 // You can set any google map options here, zoom defaults to 15
});
// Listen to button onclick event
confirmBtn.onclick = function () {
// Get current location and show it in HTML
var location = lp.getMarkerPosition();
onClickPositionView.innerHTML = '' + location.lat + ',' + location.lng;
};
// Listen to map idle event, listening to idle event more accurate than listening to ondrag event
google.maps.event.addListener(lp.map, 'idle', function (event) {
// Get current location and show it in HTML
var location = lp.getMarkerPosition();
onIdlePositionView.value = '' + location.lat + ',' + location.lng;
});
</script>
推荐阅读
- python - ALTER COLUMN "active" TYPE boolean USING "active"::boolean
- node.js - NodeJs tcp 服务器
- flutter - Flutter 程序之前运行过,没有做任何更改,它不再运行了
- r - ggplot scale_y_discrete 中的 drop=FALSE 不起作用
- perl - 读取二进制数据并将其转换为十六进制
- r - 如果您只知道 R 中的类中点,如何设置下限和上限?
- amazon-web-services - 如何查看 cloudwatch 权限?
- javascript - 如何在 Phoenix (Elixir) 渲染中使用变量注入 HTML/操作 DOM,例如动态插入 SVG 或 PNG?
- html - 如何在 CSS 中将菜单更改为堆栈?
- javascript - 我需要倒计时来慢慢倒计时