如何将javascript变量传递给html表单操作标记 [英] How to pass a javascript variable to an html form action tag

查看:151
本文介绍了如何将javascript变量传递给html表单操作标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html表单,其中包含名称,经度和纬度文本字段。经度和纬度存储在javascript变量中。你知道如何只将纬度和经度传递给服务器,我的意思是通过网址吗?每次我提交表格时,经度和纬度都不会传入网址?谁能告诉我怎么做?



这是我的javascript和html的代码:



I have an html form which contain name, longitude and latitude text field. The longitude and latitude are stored in a javascript variable. Do you know how pass only the latitude and longitude to a server, I mean via a url? Everytime I am submitting the form, the longitude and latitude are not passed into the url? Can anyone please tell me how to do that?

This is my code for both my javascript and html:

if (navigator.geolocation) {
var geolocation = {};
geolocation.latitude = 0;
geolocation.longitude = 0;
navigator.geolocation.getCurrentPosition(function (p) {
geolocation.latitude = p.coords.latitude;
geolocation.longitude = p.coords.longitude;           
}, function (error) {
alert("Failed to get GPS location");
});
} else {
alert("Failed to get GPS working");
}

function loc() {
document.getElementById("longitude").value = geolocation.latitude;
document.getElementById("latitude").value = geolocation.longitude;

}





html:





html:

<form name="photo" method="post" action="url">
<label for="name">Name:</label>
<input type="text" name="name" id="Text1" value="" />
<label for="longitude">Longitude:</label>
<input type="text" name="longitude" id="longitude" />
<label for="latitude">Latitude:</label>
<input type="text" name="latitude" id="latitude" />
<label for="image">Image:</label>
<input type="text" name="image" id="image" />
<input type="submit" value="Submit" />
</form>

推荐答案

function loc() {
---
}

function is there to在textfield中设置值,但你没有调用它。

调用这个loc()函数来设置textfield中的值。

function is there to set the value in textfield, but you did not call it.
call this "loc()" function to set the value in textfield.


这篇关于如何将javascript变量传递给html表单操作标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆