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

时间:2011-03-15 23:26:53

标签: javascript html google-maps html-form

我有一个谷歌地图,其中有一个拖动的标记。当我得到新的纬度和经度坐标时,我需要将它们作为隐藏字段放入HTML表单中。

我该怎么做?包含当前代码的示例页面位于: http://www.comehike.com/outdoors/parks/add_trailhead.php

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

将这些添加到您的表单(如果它们不存在):

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

然后,一旦你想更新值,只需:

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