[英]JS and Spring troubles
我編寫了一個小腳本,將經度和緯度返回給輸入形式的值。 在HTML中:
<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) {
document.getElementById('latitude').value = position.coords.latitude;
document.getElementById('longitude').value = position.coords.longitude;
}
</script>
<input type="text" id="latitude" />
<input type="text" id="longitude" />
有用。 但是我想讓它在春季運行。 我正在嘗試這樣做:
<p id="demo">Click the button to get your coordinates:</p>
<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) {
document.getElementById('latitude').value = position.coords.latitude;
document.getElementById('longitude').value = position.coords.longitude;
}
</script>
<form:form method="POST" commandName="geolocation">
<form:input path="latitude" id="latitude" type="text" />
<form:input path="longitude" id="longitude" type="text" />
<input id="bigbutton" type="submit" onclick="getLocation()" value="Confirm" />
</form:form>
但是它返回我零。 怎么了? 這是我的表單類:
private double latitude;
private double longitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
控制器:
@RequestMapping(value = "/about", method = RequestMethod.GET)
public String getAboutPage(Map<String, Object> map,
@ModelAttribute("geolocation") GeoLocation geolocation,
@ModelAttribute("search") SearchForm query, BindingResult result) {
map.put("news", new News());
map.put("newsList", newsService.getAboutPage());
map.put("temp", TEMP);
return "about";
}
@RequestMapping(value = "/about", method = RequestMethod.POST)
public String getAboutPageProcess(Map<String, Object> map,
@ModelAttribute("geolocation") GeoLocation geolocation,
@ModelAttribute("search") SearchForm query, BindingResult result) {
System.out.println(geolocation.getLatitude());
System.out.println(geolocation.getLongitude());
map.put("news", new News());
map.put("newsList", newsService.getAboutPage());
map.put("temp", TEMP);
return "about";
}
使用onsubmit
並使getLocation
返回false,然后在showPosition
手動提交表單,或使用jQuery / YUI / something將getLocation
附加為非破壞性事件處理程序。
編輯:也許是這樣的: http : //jsfiddle.net/mz6zN/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.