首页 > 解决方案 > 如何在 PageLoad 上获取 HiddenField 值(后面的代码)

问题描述

我使用 Java 脚本在隐藏字段上设置值,我想在中继器上为数据源使用隐藏字段值,但我有空值,因为我需要首先运行 Java 脚本并在页面加载时获取值。怎么可能?我的 Javascript 代码是:

<script type="text/javascript">
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function showPosition(position) {
                //x.innerHTML = "Latitude: " + position.coords.latitude +
                //"<br>Longitude: " + position.coords.longitude;
                //console.log("Latitude: " + position.coords.latitude +
                //"<br>Longitude: " + position.coords.longitude);

                var lat = position.coords.latitude;

                document.getElementById('<%= hdfLat.ClientID %>').value = lat;

                var lon = position.coords.longitude;

                document.getElementById('<%= hdfLong.ClientID %>').value = lon; 
            });                 
        } else {
            var error = "Geolocation is not supported by this browser.";
            alert(error);
        }
    }
</script>

标签: javascriptc#asp.nethiddenfield

解决方案


看起来您可以调用您的函数或使其成为自执行函数。

获取位置();


推荐阅读