简体   繁体   English

如何在 javascript 中获取 asp 标签控件的值?

[英]how to get value in javascript for asp Label control?

Suppose I have asp Lable set in markup like:假设我在标记中设置了 asp Lable,例如:

<asp:Label ID="myID" runat="server"></asp:Label>

then set value for this Label in code behind like:然后在后面的代码中为此标签设置值,例如:

myID.Text =100

then I want get the value 100 in javascript.然后我想在 javascript 中获取值 100。 I tried:我试过:

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

but is not working.但不工作。 How to resolve this issue?如何解决这个问题?

I believe that labels render as spans.我相信标签呈现为跨度。 Try getting the inner text.尝试获取内部文本。

document.getElementById('<%=myID.ClientID%>').innerText;

With jquery you need to use the html property.使用 jquery,您需要使用 html 属性。

var g = $('#<%=myID.ClientID%>').html();

These will NOT work with jquery这些不适用于 jquery

  • $('#<%=myID.ClientID%>').innerText $('#<%=myID.ClientID%>').innerText
  • $('#<%=myID.ClientID%>').innerHTML $('#<%=myID.ClientID%>').innerHTML
  • $('#<%=myID.ClientID%>').val() $('#<%=myID.ClientID%>').val()
var label=$("#<%myID.ClientID%>").html();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM