获取使用jQuery标签的文本 [英] Get text of label with jquery
本文介绍了获取使用jQuery标签的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做很简单的事情,但我不是成功。我有按钮和标签我的asp.net页面上,我想点击按钮后,得到的标签文本。这里是我的code:
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:
<%@ 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>
推荐答案
试试这个:
var g = $('#<%=Label1.ClientID%>').val();
或本
var g = $('#<%=Label1.ClientID%>').html();
您缺少#
在头部分补充一点:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
这篇关于获取使用jQuery标签的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文