【问题标题】:Getting Empty Values Using Javascript使用 Javascript 获取空值
【发布时间】:2017-08-01 10:33:50
【问题描述】:

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

关注 OnClientClick="call(); return false;"

<asp:Button ID="BTN_Register" runat="server" Text="Register" OnClientClick="call();return false;" CssClass="btn btn-theme-bg btn-3d btn-xs" OnClick="BTN_Register_Click" />

所以回到我的问题.. 我使用的 Javascript 代码与我在另一个 WebForm 中使用的相同。它的过程是检索纬度和经度,然后将其解析为2个asp:HiddenFields,然后使用后面的代码(aspx.cs)将它们保存到数据库中,

resultRecord = al.createLogEntry(username, externalIP, Convert.ToDouble(latitudeTB.Value), Convert.ToDouble(longitudeTB.Value), "Pending");

现在在 HTML 端用不同的 WebForm 编写,

这些是我的代码,

<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>

在 Page_Load 中,我使用此 RegisterClientScriptBlock 实际导致 HiddenFields 分别填充纬度和经度,然后使用与我在上面发布的其他 WebForm 相同的方法将其保存到数据库中,

ScriptManager.RegisterClientScriptBlock(this, GetType(), "StartGeo", "call();", true);

    Debug.WriteLine(latitudeTB.Value);
    Debug.WriteLine(longitudeTB.Value);

我希望我的 经度和纬度 值在 页面加载的那一刻出现,但似乎无济于事。我检查了 HiddenFields 的值,但我'我什么也没得到。这意味着 HiddenFields 是空的还是 null?

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

感谢任何帮助..谢谢!

【问题讨论】:

  • 有几个可能的问题。 1) 您的代码没有调用 getLocation()。 2) 它实际上并没有将值写入隐藏字段,或者 3) 当您签入 Page_Load 时它们没有被保存。只是为了仔细检查......你确定你的 getLocation() 方法被调用了吗?它实际上是在写值吗?
  • 我没有太多的 javascript 经验,或者我以前从未学过.. 不幸的是,我真的说不出来.. 我该如何测试?我实际上只是使用 Debug.WriteLine 来检查 HiddenFields 内部是否有值..
  • @EricBurdo 我试过你提供的链接,但它仍然没有得到任何值..

标签: javascript c# asp.net .net null


【解决方案1】:

你放错了变量。将“纬度”和“经度”更改为 lat 和 lng。检查下面的编辑代码。

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;

【讨论】:

  • 谢谢 Sanil,没有意识到.. 但无济于事,我的 2 HiddenFields 的调试中仍然有空值..
  • 嗨 @domster 可以在代码 sn-p var lat = position.coords.latitude; 下放置一个 alert(lat+","+lng) var lng = position.coords.longitude;只是为了检查它是否在调用 navigator.geolocation.getCurrentPosition() 函数
  • @domster navigator.geolocation.getCurrentPosition 只能通过 https 工作。但它适用于本地主机。如果您使用的是 Chrome 浏览器但无法正常工作,请尝试在 Firefox 浏览器中运行您的代码。它肯定会奏效。但请记住,地理位置是基于 HTML5 的 API,因此请将您的浏览器更新到最新版本。
猜你喜欢
  • 1970-01-01
  • 2011-09-14
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
相关资源
最近更新 更多