douhan1992 2016-10-28 07:44 采纳率: 100%
浏览 484
已采纳

window.location.href无效[关闭]

I'm using this script on javascript to change location and get some variable in the url but window.location.href didn't work i can't redirect to another page

during some search i found i need to put return false;

i put it but also i don't have any result

R: No error message appear

How to solve that ?

function getPosition() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successPosition);
  } else {
    document.getElementById("result").innerHTML = "Your browser does not support geo location api"
  }

  function successPosition(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    document.getElementById("result").innerHTML = "Latitude:" + lat + "<br/>Longitude:" + long
    window.location.href = "facee.php?lat=" + lat + "&long=" + long;
  }
  return false;
}
<form method="POST" action="" enctype="multipart/form-data" onsubmit="return getPosition();">
  <input type="file" name="image" accept="image/*" capture="camera" class="filestyle" data-buttonBefore="true" data-iconName="glyphicon glyphicon-camera" data-buttonText="Take apicture." />
  </div>
  <div class="col-md-12"></div>
  <div class="col-md-4">
    <input type="submit" name="Add" value="Add" class="btn btn-success">
</form>
  • 写回答

5条回答 默认 最新

  • dtxa49711 2016-10-28 08:36
    关注

    After reading your comments, I THINK you want this

    function successPosition(position) {
      var lat = position.coords.latitude,
        long = position.coords.longitude,
        form = document.getElementById("myForm");
      document.getElementById("result").innerHTML = "Submitting Latitude:" + lat + "<br/>Longitude:" + long;
      form.lat.value = lat;
      form.long.value = long;
      form.submit(); // HERE we submit
    };
    
    function getPosition() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successPosition);
      } else {
        document.getElementById("result").innerHTML = "Your browser does not support geo location api ";
      }
      return false; // always block submission
    }
    

    using

    <form id="myForm" method="POST" action="facee.php" 
    enctype="multipart/form-data" onsubmit="return getPosition();">
      <input type="hidden" name="lat" value="" />
      <input type="hidden" name="long" value="" />
      <input type="file" name="image" accept="image/*" capture="camera" class="filestyle" data-buttonBefore="true" data-iconName="glyphicon glyphicon-camera" data-buttonText="Take apicture." />
      </div>
      <div class="col-md-12"></div>
      <div class="col-md-4">
        <input type="submit" name="Add" value="Add" class="btn btn-success">
    </form>
    <span id="result"></span>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 Anaconda navigator updateor一直更新
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT