2013-10-03 4 views
1

에 입력하십시오. 따라서 onClick 이후 geolocation에서 위도와 경도를 생성하는 스크립트가 있습니다. 이제 innerHTML 대신 onClick 뒤에 사용자 좌표 값 필드에 배치 된 좌표가 필요합니다.JavaScript 데이터 결과를 양식 입력 값

어떻게해야합니까? 그래도 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> 

이 반복하려면 내가 생성 된 위도 및 LNG 결과를 싶습니다 자동 수동으로 사용자가 없습니다 때문에의에 빈 '값'필드에 배치하는 상자에 넣으십시오.

어떤 도움이 아주 많이 감사 :) 폼 요소의 value는, 입력이 innerHTML이없는 설정

답변

0

요소를 가져 와서 값을 추가 하시겠습니까?

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; 
} 

FIDDLE

+0

아, 작동합니다, 고마워요! –

0

기본 자바 스크립트 (101).

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

아 작동, 감사합니다! –