javascript 通过隐藏字段传递值
我有一个页面,每 x 秒使用
<meta http-equiv="refresh" content="60"/>
标签重新加载一次。页面上有一个谷歌地图,我需要记住刷新之间的缩放级别(和中心)。
这就是我所拥有的:
function initialize() {
// initialize the map
var latlng = new google.maps.LatLng(0, 0);
var myOptions = {
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
myMarkers = new Array();
updateMarkers();
cen = new google.maps.LatLng(-12.461334, 130.841904);
map.setCenter(cen);
zoomLevel = document.getElementById("zoomLevel").value;
map.setZoom(parseInt(zoomLevel));
google.maps.event.addListener(map, 'zoom_changed', function() {
str = map.getZoom() + '';
document.getElementById("zoomLevel").value = str;
});
}
所以,我有一个事件侦听器,它将在每次缩放更改后更新隐藏值 ZoomLevel。每次重新加载时,应从该值读取缩放级别。
<input type="hidden" name="zoomLevel" id="zoomLevel" value="4" />
但每次刷新后,缩放级别都会设置为 4。我做错了什么?
谢谢。
I have a page that reloads it self every x seconds with
<meta http-equiv="refresh" content="60"/>
tag. There is a google map on the page and I need to remember the zoom level (and center) between refreshes.
This is what I have:
function initialize() {
// initialize the map
var latlng = new google.maps.LatLng(0, 0);
var myOptions = {
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
myMarkers = new Array();
updateMarkers();
cen = new google.maps.LatLng(-12.461334, 130.841904);
map.setCenter(cen);
zoomLevel = document.getElementById("zoomLevel").value;
map.setZoom(parseInt(zoomLevel));
google.maps.event.addListener(map, 'zoom_changed', function() {
str = map.getZoom() + '';
document.getElementById("zoomLevel").value = str;
});
}
So, I have a event listener that will update a hidden value zoomLevel after each zoom change. On each reload zoom level should be read from that value.
<input type="hidden" name="zoomLevel" id="zoomLevel" value="4" />
But after each refresh the zoom level is set to 4. What am I doing wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
元刷新不是回发,而是页面的实际清理请求。您有两种方法可以解决此问题:
setTimeout
代替并调用form.submit
(或者构建__doPostBack
链接,如果你使用的是 asp.网)。这样您就可以发出实际的发布请求。The meta refresh isn't a postback, it's an actual clean request of the page. You have 2 ways to fix this:
setTimeout
instead and callform.submit
(or build the__doPostBack
link if you're using asp.net). This way you make an actual post request.我相信解决方案就在您的指尖。事实上你根本不需要回发。只需安排initialize() 函数每60 秒调用一次即可。所以 -
就是这样。
I believe that the solution is right at your fingertips. In fact you do not need to postback at all. Just schedule the initialize() function to be called every 60 seconds. So -
There you are.
也许您可以使用 cookie 来记住页面的状态。
Maybe you can use cookies to remember the state of your page.