简体   繁体   English

在单击按钮时使用Javascript清除多个ASP文本框

[英]Clear multiple ASP Textboxes using Javascript on Button Click

I am trying to clear data of multiple ASP Textboxes on clicking Clear button. 我试图在单击“ 清除”按钮时清除多个ASP文本框的数据。 I was trying with one textbox initially. 我最初尝试使用一个文本框。 It is clearing the the textbox data on button-click, but it is also calling the Page_Load method. 它是在单击按钮时清除文本框数据的,但是它也在调用Page_Load方法。 I want to clear textboxes without calling Page_Load method. 我想清除文本框而不调用Page_Load方法。

Here is the code, I tried: 这是我尝试的代码:

<body>
    <form id="form1" runat="server">
                <script type="text/javascript">
                    function Clear() {
                        document.getElementById("<%=txt.ClientID %>").value = "";
                return true;
            }
   </script>
    <div>

        <asp:TextBox ID="txt" runat="server"></asp:TextBox>
        <asp:Button ID="btn" runat="server" Text="click" OnClientClick="return Clear();" />    
    </div>
    </form>
</body>

You can avoid page load by changing the return of your function: 您可以通过更改函数的返回值来避免页面加载:

function Clear() {
    document.getElementById("<%=txt.ClientID %>").value = "";
    return false;
}

And you can use classes to clear multiple textboxes, like this: 您可以使用类来清除多个文本框,如下所示:

<asp:TextBox ID="txt1" runat="server" CssClass="txts"></asp:TextBox>
<asp:TextBox ID="txt2" runat="server" CssClass="txts"></asp:TextBox>
<asp:TextBox ID="txt3" runat="server" CssClass="txts"></asp:TextBox>
<asp:TextBox ID="txt4" runat="server" CssClass="txts"></asp:TextBox>

And the JS: 和JS:

function Clear() {
    var txts = document.getElementsByClassName('txts');

    for (var i = 0; i < txts.length; i++)
        txts[i].value = '';

    return false;
}

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM