簡體   English   中英

驗證表單中按按鈕清除標簽

[英]Clear a label by button in a form with validation

使用TextBox1Button1Label1在非常簡單的Web TextBox1重新創建了我的問題。 我在TextBox1上使用“驗證”(客戶端),因此如果為空,則會顯示一條消息。

在后面的代碼(服務器端)中-設置了Label1

我使用javascript清除Label1 ,但是它不起作用。

<head runat="server"><title></title>
     <script type="text/javascript">
         function ClearLabel() { $('#Label1').val(""); }
     </script>
</head>
<body><form id="form1" runat="server">
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
           <asp:RequiredFieldValidator ID="RFV_1" runat="server"
              ErrorMessage="pls enter value" Text="*" ControlToValidate="TextBox1">
           </asp:RequiredFieldValidator>
       <asp:Label ID="Label1" runat="server"></asp:Label>
       <asp:Button ID="Button1" runat="server" Text="Save" 
           OnClientClick="ClearLabel()" 
           OnClick="Button1_Click" />
       <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" />
</form></body></html>

后面的代碼:

    protected void Button1_Click(object sender, EventArgs e)
    {if (Page.IsValid)
        {Label1.Text = ("value is : " + TextBox1.Text);}}

清除Label1的JavaScript未觸發或無法正常工作。
我要去哪里錯...?

替換$('#Label1').val(""); $('#Label1').text("") 使用JQuery設置標簽文本

編輯:

或者,您可以使用此document.getElementById("#Label1").innerHTML=''; 而不是$('#Label1').text("") (jquery代碼)

這是我解決的方法-
在后面的代碼中,我添加了“ else”語句:

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        { 
            Label1.Text = ("value is : " + TextBox1.Text); 
        }
        else
        {
            Label1.Text = "";
        }
    }

在我的html中,我完全刪除了javascript,但仍使用函數填充了OnClientClick內容,如下所示:

<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RFV_1" runat="server"
        ErrorMessage="pls enter value" Text="*" ControlToValidate="TextBox1">
    </asp:RequiredFieldValidator>
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Save"
        OnClientClick="blablabla"
        OnClick="Button1_Click" />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" />
</form>
</body>

請注意它調用的不存在的偽函數的名稱,否則將無法使用:

    OnClientClick="blablabla"

它工作正常且符合預期,盡管我不知道它是如何做到的。 因此,我找到了一些教程,並將在接下來的兩天里花時間了解javascrit。
謝謝你們!
加迪

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM