使用Javascript获取空值

使用Javascript获取空值,javascript,c#,asp.net,.net,null,Javascript,C#,Asp.net,.net,Null,我正在尝试使用地理定位获取值(纬度、经度)。我尝试在另一个页面中使用相同的Javascript(我将在问题下方发布),但此Javascript仅在单击按钮时触发 关注OnClientClick=“call();返回false;” 现在在HTML端用不同的Web表单编写 这是我的密码 <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server"> <p class="text-succes

我正在尝试使用地理定位获取值(纬度、经度)。我尝试在另一个页面中使用相同的Javascript(我将在问题下方发布),但此Javascript仅在单击按钮时触发

关注OnClientClick=“call();返回false;”

现在在HTML端用不同的Web表单编写

这是我的密码

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<p class="text-success" runat="server" id="loginDetails"></p>
<p class="text-danger" runat="server" id="logoutDetails"></p>

<div>
    <asp:HiddenField runat="server" ID="latitudeTB"/>
    <asp:HiddenField runat="server" ID="longitudeTB"/>

    <script type="text/javascript">
    //GeoLocation Retrieval
    var geo = document.getElementById("geolocationValue");
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                document.getElementById("<%=latitudeTB.ClientID %>").value = lat;
                document.getElementById("<%=longitudeTB.ClientID %>").value = lng;
            });

        } else {
            geo.innerHTML = "Geolocation is not supported by this browser.";
        }
    }
    function call() {
        getLocation();
    }

    /*document.onreadystatechange = function (e) {
        if (document.readyState === 'complete') {
            //dom is ready, window.onload fires later
            call();
        }
    };*/

    /*$(window).load(function () {
        call();
    });*/
</script>

</div>
我希望我的经度和纬度值在页面加载的时刻出现,但似乎没有用。。我检查了HiddenFields的值,但没有得到任何结果。。这意味着HiddenFields是空的还是空的

我能知道我能做些什么来解决这个问题吗


谢谢你的帮助。。谢谢大家!

您输入了错误的变量。将“纬度”和“经度”更改为纬度和经度。检查下面编辑的代码。

var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("<%=latitudeTB.ClientID %>").value = lat;
document.getElementById("<%=longitudeTB.ClientID %>").value = lng;
var lat=position.coords.lation;
var lng=位置坐标经度;
document.getElementById(“”).value=lat;
document.getElementById(“”).value=lng;
您以前的代码是

var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("<%=latitudeTB.ClientID %>").value = latitude;
document.getElementById("<%=longitudeTB.ClientID %>").value = longitude;
var lat=position.coords.lation;
var lng=位置坐标经度;
document.getElementById(“”)。值=纬度;
document.getElementById(“”)。值=经度;

谢谢Sanil,我没有意识到。。但是没有用,我在调试中仍然得到了2个隐藏字段的空值..Hi@domster可以在代码段var lat=position.coords.latitude下面放置一个警报(lat+,“+lng);var lng=位置坐标经度;只是为了检查它是否正在调用navigator.geolocation.getCurrentPosition()function@domsternavigator.geolocation.getCurrentPosition将仅通过https工作。但它将适用于localhost。如果您使用的是Chrome浏览器,但不工作,请尝试在Firefox浏览器中运行代码。这肯定会奏效的。但是请记住地理位置是基于HTML5的API,所以请将浏览器更新为最新版本。1) 您的代码没有调用getLocation()。2) 它实际上并没有将值写入隐藏字段,或者3)当您签入页面加载时,这些值没有被保存。只是想再核实一下。。。您确定正在调用getLocation()方法吗?它实际上是在写值吗?这有帮助吗:我没有太多的javascript经验,或者我以前从未学过。。不幸的是,我真的说不出来。。我怎样才能测试?实际上,我只是使用Debug.WriteLine检查HiddenFields中是否有值。@EricBurdo我尝试了您提供的链接,但仍然没有得到任何值。。
ScriptManager.RegisterClientScriptBlock(this, GetType(), "StartGeo", "call();", true);

    Debug.WriteLine(latitudeTB.Value);
    Debug.WriteLine(longitudeTB.Value);
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("<%=latitudeTB.ClientID %>").value = lat;
document.getElementById("<%=longitudeTB.ClientID %>").value = lng;
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("<%=latitudeTB.ClientID %>").value = latitude;
document.getElementById("<%=longitudeTB.ClientID %>").value = longitude;