我有一個按鈕來過濾搜索結果到最近的位置。按鈕與其他參數(NOT AJAX)
我的HTML按鈕
<a href="trending" onclick="addUrl(this); alert(this);" class="btn btn-warning" id="location">Nearest Location</a>
我的jQuery
function addUrl(element){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
console.log(lat);
console.log(lng);
$(element).attr('href',function(){
return element.href + '?lat=' + lat + '&lng=' + lng;
});
});
}
else
alert("navigator.geolocation is not available");
}
當我按一下按鈕,它似乎導航到URL不帶參數。不知道這裏有什麼問題。
如果使用的preventDefault()我怎麼瀏覽網頁與新添加的參數? –
正如我提到的使用'window.location.href = YOUR_URL_WITH_PARAMS' - 設置這使得重定向。你可以在這裏閱讀更多:https://developer.mozilla.org/en-US/docs/Web/API/Window/location –
是我不好,是的,它的工作原理的preventDefault感謝後 –