Html表单-输入等于“时运行Javascript”;“附近”;

Html表单-输入等于“时运行Javascript”;“附近”;,javascript,jquery,html,forms,geolocation,Javascript,Jquery,Html,Forms,Geolocation,我有一些javascript,可以从他们的设备获取用户的纬度和经度,用于本地搜索 一切都正常运行,但由于geolocation函数会向用户创建一个弹出警告,提示用户“允许”或“不允许”使用其位置,因此我不希望运行脚本,除非用户特别选择“附近”作为位置 下面是我的java,它可以进行地理定位: <script type="text/javascript"> x = navigator.geolocation; x.getCurrentPosition(success, failure

我有一些javascript,可以从他们的设备获取用户的纬度和经度,用于本地搜索

一切都正常运行,但由于geolocation函数会向用户创建一个弹出警告,提示用户“允许”或“不允许”使用其位置,因此我不希望运行脚本,除非用户特别选择“附近”作为位置

下面是我的java,它可以进行地理定位:

<script type="text/javascript">

x = navigator.geolocation;
x.getCurrentPosition(success, failure);

function success(position){
    document.getElementById('lat1').value = position.coords.latitude;
    document.getElementById('long1').value = position.coords.longitude;
}
function failure()
{
// do nothing
}
因此,总而言之,我希望脚本仅在search-box-2字段值等于'nearly'时运行


提前谢谢。

您是否尝试过类似的方法:

在提交时触发此功能

function notNearby() {
  if($('#search-box-2').val() != 'nearby') {  
    x = navigator.geolocation;
    x.getCurrentPosition(success, failure);

    function success(position){
        document.getElementById('lat1').value = position.coords.latitude;
        document.getElementById('long1').value = position.coords.longitude;
    }
    function failure()
    {
    // do nothing
    }
  }
}
HTML:

搜索
不是这样,就是我不明白你的问题

function notNearby() {
  if($('#search-box-2').val() != 'nearby') {  
    x = navigator.geolocation;
    x.getCurrentPosition(success, failure);

    function success(position){
        document.getElementById('lat1').value = position.coords.latitude;
        document.getElementById('long1').value = position.coords.longitude;
    }
    function failure()
    {
    // do nothing
    }
  }
}
 <button class="button-homepage" onclick='notNearby();' type="submit">Search</button>