【问题标题】:innerHtml with ASP.NET and JQuery Mobile带有 ASP.NET 和 JQuery Mobile 的 innerHtml
【发布时间】:2012-07-31 13:20:59
【问题描述】:

我正在进行移动搜索,它使用您的位置来确定邮政编码服务器端,但是当尝试将两个标签设置为纬度和经度以发送到服务器时,我收到一个错误,声称 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在运行时生成的,我调试的时候看页面源码就可以确认。

有什么帮助吗?

【问题讨论】:

  • 您的代码中存在语法错误,innnerHtmlknnerHtml。你应该用更正的来改变它们 -> innerHTML
  • 这是复制粘贴的吗?因为document.getElementById("ct100_ContentPlaceHolder1_lblLong").knnerHtml = longitude; 应该是document.getElementById("ct100_ContentPlaceHolder1_lblLong").innerHtml = longitude;
  • 抱歉,修复了,我复制了,但是根据朋友的建议更改了 .html vs .innerHtml。

标签: javascript asp.net html jquery-mobile


【解决方案1】:

在您的 getLocation() 函数中,您是否可以尝试执行以下操作:

function getLocation() {
    $('#<%=lblLat.ClientID%>').html(latitude);
    $('#<%=lblLong.ClientID%>').html(latitude);
}

您也可以尝试以类似的方式设置两个标签的文本属性:

function getLocation() {
    $('#<%=lblLat.ClientID%>').text(latitude);
    $('#<%=lblLong.ClientID%>').text(latitude);
}

【讨论】:

  • 好的,$('#').text 工作,但是现在当我尝试在服务器端获取 lblLat/lblLong.Text 时,它返回一个空字符串,有什么想法吗?
  • 如果您希望从服务器端代码中获得值,您可能还需要设置隐藏输入字段的值。这是一个类似的帖子:stackoverflow.com/questions/2493209/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多