我正在进行移动搜索,它使用您的位置来确定邮政编码服务器端,但是当尝试将两个标签设置为纬度和经度以发送到服务器时,我收到一个错误,声称 innerHtml 为空。经过进一步检查,该元素为空。为什么会这样?
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Search.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script language="javascript" type="text/javascript">
navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
var latitude;
var longitude;
function foundLocation(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
alert('Your location: ' + latitude + ', ' + longitude);
}
function noLocation() {
//Do something here in the case that no location is found, or user denies access
}
function getLocation() {
document.getElementById("ct100_ContentPlaceHolder1_lblLat").innerHtml = latitude;
document.getElementById("ct100_ContentPlaceHolder1_lblLong").innerHtml = longitude;
}
</script>
<div data-role="page">
<div data-role="header">
<h1>Provider Search</h1>
</div>
<div data-role="content">
<asp:Button ID="btnSearch" Text="Search" runat="server" data-role="button" OnClientClick="getLocation()" OnClick="btnSearch_Clicked"></asp:Button>
<asp:Label ID="lblLat" runat="server"></asp:Label>
<asp:Label ID="lblLong" runat="server"></asp:Label>
<p></p>
</div>
</div>
</asp:Content>
我想指出,除了标签的设置之外,本文档中的所有内容都运行良好。
还有,id前缀ct100_ContentPlaceHolder1_是asp.net在运行时生成的,我调试的时候看页面源码就可以确认。
有什么帮助吗?