我有一些javascript,它从用户设备中获取用户的纬度和经度,用于本地搜索。
一切都正常运行,但是由于地理位置函数创建了一个弹出警告,警告用户“允许”或“不允许”使用它们的位置,除非用户专门选择“附近”作为位置,否则我不想运行脚本。
下面是我的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
}
这是我的表格。输入id="search-box-2"是我想要用来改变脚本行为的输入元素。如果input id="search-box-2"有value="Nearby",那么我希望执行javascript函数。否则,理想情况下,它将什么也不做。
<form id="search-form" method="get" accept-charset="UTF-8, utf-8" action="page.php">
<fieldset>
<input id="search-box-1" name="search" type="text" placeholder="" />
<input name="lat" type="text" id="lat1" value="" />
<input name="long" type="text" id="long1" value="" />
<input id="search-box-2" name="where" type="text" placeholder="City or State" value=""/>
<div class="button-holder">
<button class="button-homepage" type="submit">Search</button>
</div>
</fieldset>
</form>因此,总之,只有在搜索框-2字段值等于“附近”的情况下,我才希望脚本运行。
提前谢谢。
发布于 2014-09-04 22:45:02
你试过这样的方法吗?
在提交时触发此函数
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:
<button class="button-homepage" onclick='notNearby();' type="submit">Search</button>要么就是我不明白你的问题
https://stackoverflow.com/questions/25676031
复制相似问题