使用 jquery 获取标签文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6972666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get text of label with jquery
提问by Rougher
I want to do very simple thing, but I'm not success. I have button and label on my asp.net page and I want to get text of label after clicking on button. Here is my code:
我想做很简单的事情,但我没有成功。我的 asp.net 页面上有按钮和标签,我想在单击按钮后获取标签文本。这是我的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="DynamicWebApplication.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function f()
{
var g = $('<%=Label1.ClientID%>').val(); // Also I tried .text() and .html()
alert(g);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<p></p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="f();"/>
</div>
</form>
</body>
回答by kleinohad
try this:
尝试这个:
var g = $('#<%=Label1.ClientID%>').val();
or this:
或这个:
var g = $('#<%=Label1.ClientID%>').html();
you are missing the #
你错过了#
add this in the head section:
在 head 部分添加:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
回答by ShankarSangoli
Try this
尝试这个
var g = $('#<%=Label1.ClientID%>').text();
回答by beatgammit
Try using the html() function.
尝试使用 html() 函数。
$('#<%=Label1.ClientID%>').html();
$('#<%=Label1.ClientID%>').html();
You're also missing the # to make it an ID you're searching for. Without the #, it's looking for a tag type.
您还缺少 # 以使其成为您正在搜索的 ID。没有#,它正在寻找一个标签类型。
回答by Rajnikant
try document.getElementById('<%=Label1.ClientID%>').text or innerHTML OTHERWISE LOAD JQUERY SCRIPT AND put your code as it is....
尝试 document.getElementById('<%=Label1.ClientID%>').text 或 innerHTML 否则加载 JQUERY 脚本并按原样放置您的代码......
回答by ANeme
Try:
尝试:
<%=this.Label1.Text%>
回答by DrCJones
No solution here worked for me. Instead I added a class to the label and was able to select it that way.
这里没有解决方案对我有用。相反,我向标签添加了一个类,并且能够以这种方式选择它。
<asp:Label ID="Label1" CssClass="myLabel1Class" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label1" CssClass="myLabel1Class" runat="server" Text="Label"></asp:Label>
$(".myLabel1Class").val()
$(".myLabel1Class").val()
And, as mentioned by others, make sure you have your jquery loaded.
而且,正如其他人所提到的,请确保您加载了 jquery。
回答by Omar Kamel
for the line you wrote
对于你写的那一行
var g = $('<%=Label1.ClientID%>').val(); // Also I tried .text() and .html()
var g = $('<%=Label1.ClientID%>').val(); // 我也试过 .text() 和 .html()
you missed adding #. it should be like this
你错过了添加#。应该是这样的
var g = $('#<%=Label1.ClientID%>').text();
var g = $('#<%=Label1.ClientID%>').text();
also I do not prefer using this method
我也不喜欢使用这种方法
that's because if you are calling a control in master or nested master page or if you are calling a control in page from master. Also controls in Repeater. regardless the MVC. this will cause problems.
那是因为如果您在母版或嵌套母版页中调用控件,或者如果您从母版调用页面中的控件。还控制中继器。不管MVC。这会导致问题。
you should ALWAYS call the ID of the control directly. like this
您应该始终直接调用控件的 ID。像这样
$('#ControlID')
$('#ControlID')
this is simple and clear. but do not forget to set
这很简单明了。但不要忘记设置
ClientIDMode="Static"
ClientIDMode="静态"
in your controls to remain with same ID name after render. that's because ASP.net will modify the ID name in HTML rendered file in some contexts i.e. the page is for Master page the control name will be ConetentPlaceholderName_controlID
在您的控件中渲染后保持相同的 ID 名称。那是因为 ASP.net 将在某些上下文中修改 HTML 呈现文件中的 ID 名称,即页面用于母版页,控件名称将为 ConetentPlaceholderName_controlID
I hope it clears the question Good Luck
我希望它清除了这个问题祝你好运
回答by Maysam
It's simple, set a specific value for that label (XXXXXXX for example) and run it, open html source of output (in browser) and look for XXXXXXX, you will see something like this <span id="mylabel">XXXXXX</span>
it's what you want, the ID of <span>
(I think it's usually same as Label name in asp code) now you can get its value by innerHTML or another method in JQuery
很简单,为该标签设置一个特定的值(例如 XXXXXXX)并运行它,打开输出的 html 源(在浏览器中)并查找 XXXXXXX,你会看到这样的东西,<span id="mylabel">XXXXXX</span>
它是你想要的,ID <span>
(我认为它通常与asp代码中的标签名称相同)现在您可以通过innerHTML或JQuery中的其他方法获取其值