jQuery 如何使用jQuery将文本设置为标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6986504/
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
How to set text to label with jQuery?
提问by Rougher
I want to set text to label with jQuery after clicking on button. I wrote code and it works, but after I set text in my label, label return his old state. Here is my code:
单击按钮后,我想使用 jQuery 将文本设置为标签。我编写了代码并且它可以工作,但是在我的标签中设置文本后,标签返回他的旧状态。这是我的代码:
<%@ 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 src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function f()
{
$('#<%=Label1.ClientID%>').html("hello");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
<p></p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="f();"/>
</div>
</form>
</body>
</html>
回答by ipr101
If your button is causing a postback then the changes will be lost after the page has reloaded. Try this -
如果您的按钮导致回发,那么在页面重新加载后更改将丢失。尝试这个 -
function f()
{
$('#<%=Label1.ClientID%>').html("hello");
return false;
}
回答by ShankarSangoli
You can use text
method to set the text
您可以使用text
方法来设置文本
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="return f();"/>
function f()
{
$('#<%=Label1.ClientID%>').html("hello");
return false;
}
回答by TGarrett
I would use this,
我会用这个,
$('#<%=Label1.ClientID%>').text("hello");
回答by rkw
Labels do not maintain viewstate. The server will not post that information back to the server. You can try explicitly enabling the ViewState on your Label, but if that doesn't work, you will have to store that value in a hidden field.
标签不维护视图状态。服务器不会将该信息发布回服务器。您可以尝试在标签上显式启用 ViewState,但如果这不起作用,则必须将该值存储在隐藏字段中。
<asp:Label ID="Label1" runat="server" EnableViewState="true"></asp:Label>
回答by Vikas Kottari
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return f();"/>
function f()
{
$('#<%=Label1.ClientID%>').html("hello");
return false;
}
OR
或者
<asp:Button ID="Button1" runat="server" Text="Button" />
$(document).ready(function(){
$('#<%=Button1.ClientID%>').click(function(){
$('#<%=Label1.ClientID%>').html("hello");
return false;
});
});