Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JavaScript数据结果放入表单输入值_Javascript_Php_Html_Forms - Fatal编程技术网

将JavaScript数据结果放入表单输入值

将JavaScript数据结果放入表单输入值,javascript,php,html,forms,Javascript,Php,Html,Forms,好的,我有一个脚本,在onClick之后从地理位置生成纬度和经度。现在,我希望这些坐标放置在onClick之后的users表单值字段中,而不是innerHTML 我该怎么做呢?我还需要使用innerHTML吗 我的表格详情: <body> <h1>Insert New Address</h1> <form id="myForm" method="post" action="index3.php"> <input type="hidden"

好的,我有一个脚本,在onClick之后从地理位置生成纬度和经度。现在,我希望这些坐标放置在onClick之后的users表单值字段中,而不是innerHTML

我该怎么做呢?我还需要使用innerHTML吗

我的表格详情:

<body>
<h1>Insert New Address</h1>
 <form id="myForm" method="post" action="index3.php">
 <input type="hidden" name="submitted" value="true">
<fieldset>
<legend>New Cafes</legend>
<label>Name<input type="text" name="name"></label>
<label>Address<input type="text" name="address"></label>
<label>Postcode<input type="text" name="address2"></label>
<label>Latitude<input type="text" name="lat" value="" id="addLat"></label>
<label>Longitude<input type="text" name="lng" value="" id="addLng"></label>
</fieldset>
<input type="submit" name="sub" value="Submit">
</form>


<p id="demo">Click the button to get your coordinates:</p>
<button onClick="getLocation()">Try It</button>

<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }
</script>

插入新地址
新咖啡馆
名称
住址
邮政编码
纬度
经度
单击按钮获取您的坐标:

试试看 var x=document.getElementById(“演示”); 函数getLocation() { if(导航器.地理位置) { navigator.geolocation.getCurrentPosition(showPosition); } else{x.innerHTML=“此浏览器不支持地理位置。”;} } 功能显示位置(位置) { x、 innerHTML=“纬度:”+position.coords.Latitude+ “
经度:”+position.coords.Longitude; }
重申一下,我希望生成的lat和lng结果自动放置在for中的空“值”字段中,这样用户就不必手动放置在框中

非常感谢您的帮助:)

基本JavaScript 101

设置表单元素的
,输入没有
innerHTML

function showPosition(position) {
    document.getElementById("addLat").value = position.coords.latitude;
    document.getElementById("addLng").value = position.coords.longitude;
}

获取元素并添加值

var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    document.getElementById('addLat').value = position.coords.latitude;
    document.getElementById('addLng').value = position.coords.longitude;
}