我希望在表单提交时检查地理位置。如果表单提交的是好的,如果没有,则不会 现在我只想得到协调,所以我有:
var x = document.getElementById("demo");
var isGoodLocation = false;
$(function() {
$('.submit').on('submit', function(event) {
getLocation();
if (!isGoodLocation) {
event.preventDefault();
event.stopPropagation();
alert("Form Submission prevented / stoped.");
}
});
});
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;
isGoodLocation = false;
}
如果我在一个简单的按钮上设置了getLocation(),它可以工作,但如果它在我的class =“submit”表单上设置,它就不会进入showPosition函数, 有谁知道为什么?
稍后编辑: @dandavis在评论中回答,谢谢