简体   繁体   English

如何将JavaScript值作为隐藏的表单字段?

[英]How to put JavaScript values as hidden form fields?

I have a google map that has a marker which is dragged. 我有一个Google地图,其中有一个被拖动的标记。 When I get the new latitude and longitude coordinates, I need to put them into an HTML form as hidden fields. 当我获得新的纬度和经度坐标时,需要将它们作为隐藏字段放入HTML表单中。

How can I do that? 我怎样才能做到这一点? The example page with current code is here: http://www.comehike.com/outdoors/parks/add_trailhead.php 当前代码示例页面位于: http : //www.comehike.com/outdoors/parks/add_trailhead.php

如果您的表单已经包含隐藏的输入字段,并且这些字段具有ID,则可以执行

document.getElementById("id of the input field").value = myVariable;

Add these to your form (if they dont exist): 将它们添加到您的表单中(如果不存在):

<input type="hidden" value="" id="lat" name="lat" />
<input type="hidden" value="" id="lng" name="lng" />

Then once you want to update the values just do: 然后,一旦要更新值,请执行以下操作:

document.getElementById("lat").setAttribute("value", newLat);
document.getElementById("lng").setAttribute("value", newLng);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM