如何使用jQuery设置文本标签?

Rou*_*her 11 asp.net jquery label set button

我想在点击按钮后用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>
Run Code Online (Sandbox Code Playgroud)

ipr*_*101 25

如果您的按钮导致回发,则重新加载页面后更改将丢失.试试这个 -

function f() 
        {
            $('#<%=Label1.ClientID%>').html("hello"); 
            return false;  
        }
Run Code Online (Sandbox Code Playgroud)