JavaScript检查值是否存在,然后提交表单
我正在使用以下代码查找浏览器位置并将其发布到下一页,但问题是JavaScript查找浏览器位置花费的时间很短,并且表单在找到浏览器位置之前发布,我如何使表单仅在找到浏览器位置后才提交 newtest1.phpJavaScript检查值是否存在,然后提交表单,javascript,php,jquery,html,forms,Javascript,Php,Jquery,Html,Forms,我正在使用以下代码查找浏览器位置并将其发布到下一页,但问题是JavaScript查找浏览器位置花费的时间很短,并且表单在找到浏览器位置之前发布,我如何使表单仅在找到浏览器位置后才提交 newtest1.php <html> <head> <script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPositi
<html>
<head>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
getLocation();
</script>
</head>
<body>
<form id="myForm" method="post" action="newtest2.php">
<input type="text" id="getlat" name="getlat" />
<input type="text" id="getlon" name="getlon" />
<input type="button" name="myformer" onclick="myFunction();" />
</form>
<script>
function myFunction() {
document.getElementById("myForm").submit();
}
myFunction();
</script>
</body>
</html>
<?php
echo $_POST['getlat'];
echo $_POST['getlon'];
?>
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}
}
功能显示位置(位置){
document.getElementById(“getlat”).value=position.coords.latitude;
document.getElementById(“getlon”).value=position.coords.longitude;
}
getLocation();
函数myFunction(){
document.getElementById(“myForm”).submit();
}
myFunction();
newtest2.php
<html>
<head>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
getLocation();
</script>
</head>
<body>
<form id="myForm" method="post" action="newtest2.php">
<input type="text" id="getlat" name="getlat" />
<input type="text" id="getlon" name="getlon" />
<input type="button" name="myformer" onclick="myFunction();" />
</form>
<script>
function myFunction() {
document.getElementById("myForm").submit();
}
myFunction();
</script>
</body>
</html>
<?php
echo $_POST['getlat'];
echo $_POST['getlon'];
?>
您可以在默认情况下禁用“提交”按钮,并在成功找到位置时删除“禁用”属性。因此,您可以防止在找到职位之前提交表单。
但更好的方法是只需按按钮触发计算并等待成功响应,而不是在需要时使用lan和lon值执行ajax请求,或者只需操纵dom以显示位置而无需重新加载。如果您的php脚本只是打印通过javascript发送的变量,则不需要php脚本。只需使用javascript在html页面上打印变量值。
geolocation.getCurrentPosition
是异步的,因此如果您想等待事情完成时发生,您应该将这些事情放在回调函数中-showPosition()
您必须重写您拥有的内容,并且不应自动调用submit函数,我假设您只在提交表单时需要它:
function myFunction() {
document.getElementById("myForm").submit();
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
// now submit the form as the position has been determined
myFunction();
}
$('#myForm').on('submit', function(e) {
e.preventDefault();
getLocation();
});
您可能可以稍微清理一下,您将问题标记为jQuery,但您似乎没有太多地使用它,您的函数名和内联事件处理程序可以改进/删除。基于您发布的代码和您的问题,我认为这里最简单的方法是等待DOM准备就绪,因为
onclick
,表单直到有人单击按钮才会提交。我建议您将代码包装到document ready
中,为什么您要对这个问题提出一些相同的问题@PrabowoMurti-如果你阅读这两个问题,你会明白代码是相同的,但问题是不同的。你可以随时编辑和改进你以前的问题,但问题是完全不同的,那是使用Ajax的,我收到的答复是它不起作用,我现在正以这种方式努力,至少要以这种方式工作。但这也不起作用。这不是一个持续发布更新的讨论论坛,如果这是新的疑问,我只剩下一个新问题。