【问题标题】:Changing Hidden Values to Geolocation in a Form on the fly动态将隐藏值更改为表单中的地理位置
【发布时间】:2012-11-09 15:08:55
【问题描述】:

我正在创建一个搜索脚本,该脚本还包含用户的纬度和经度,这也可以用作搜索的基础。我通过使用 onLoad 方法来实现这一点,该方法调用应该将代码放入表单的 javascript。

<body <?php body_class(); ?> onload="office_getLocation()">
<form name='office_address_search_home' method='post' action='http://domain.com   /address-search-test/#map' class='form-search'>
<input class='office_address_search_input_home input-medium search-query' name='office_address_search' style="height:12px;font-size: 12px;padding-top:5px;" onClick="this.value=''" value="Find Your Nearest Office" />
<input type='hidden' name='region' value='United Kingdom' />
<input type="hidden" name="office_latitude" id="office_latitude_id" value=""/>
<input type="hidden" name="office_longitude" id="office_longitude_id" value="" />
<input name='Submit' class='office_address_submit_home btn' type='submit' value='Search' style='height: 26px;line-height: 12px;' />
</form>

<script>
function office_getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}

function showPosition(position)
{
document.getElementById("office_latitude_id").value = position.coords.latitude; 
document.getElementById("office_longitude_id").value = position.coords.longitude;
}
</script>

但它不起作用。

我已将 showPosition(position) 的结果(位置.coords.latitude 和经度)回显到屏幕上,并且没有问题,只是没有将它们分配给隐藏值。

有什么想法吗?如果需要,我们很乐意提供更多数据。

干杯

【问题讨论】:

  • 请检查您发布的代码。这看起来很可疑:navigator.geolocation.getCurrentPosition(showPosition);

标签: javascript wordpress google-maps geolocation


【解决方案1】:

我想通了:)

基本上,代码在上面工作。但是更新的地理位置值没有出现在源代码中。我通过将隐藏变量更改为文本框来测试这一点,并且 - 最终(在页面完成加载后一秒钟左右)它们出现了。此更改未反映在源代码中,但确实有效:)

不确定这对任何人有多大用处,但你去吧:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多