简体   繁体   English

如何将javascript变量传递给html表单操作标签

[英]How to pass a javascript variable to an html form action tag

I have an html form which contain name, longitude and latitude text field. 我有一个HTML表单,其中包含名称,经度和纬度文本字段。 The longitude and latitude are stored in a javascript variable. 经度和纬度存储在javascript变量中。 Do you know how post the name, 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? 每次我提交表单时,经度和纬度都不会传递到url中吗? Can anyone please tell me how to do that? 谁能告诉我该怎么做?

Thank you 谢谢

This is my code for both my javascript and html: 这是我的javascript和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>

You could add elements to the form... 您可以在表单中添加元素...

var myForm = document.getElementById('myForm');

var longitude = document.createElement('input');
longitude.name= 'longitude';
longitude.value = '100';

var latitude= document.createElement('input');
latitude.name= 'latitude';
latitude.value = '100';

myForm.appendChild(longitude);
myForm.appendChild(latitude);

In the action of the form you need to mention the name of the servlet you want to call. 在表单的操作中,您需要提及您要调用的servlet的名称。 Make use of three hidden fields to store the values name, longitude and latitude. 利用三个隐藏字段来存储值名称,经度和纬度。 This can be done using 这可以使用

document.getElementById('hiddenFieldId1').value=longitude; document.getElementById('hiddenFieldId2').value=latitude; document.getElementById('hiddenFieldId3').value=name;

These values can be referred in the servlet using request.getParameter('hiddenFieldId'); 可以使用request.getParameter('hiddenFieldId');在servlet中引用这些值request.getParameter('hiddenFieldId');

You need to submit the form in your js: 您需要在js中提交表单:

document.myform.submit();

geolocation variable hasn't been made global, so it is not defined in your function loc() . geolocation变量尚未设为全局变量,因此未在函数loc()定义。 There are no values assigned to geolocation.latitude and geolocation.longitude . 没有为geolocation.latitudegeolocation.longitude分配任何值。

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

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