繁体   English   中英

如何禁用按钮,然后在textbox.textchanged事件后启用它?

[英]How to disable button and then enable it after textbox.textchanged event?

我有一个用户控件,作为更新学生记录的表单。 此用户控件用于名为学生更新的页面中。 更新成功后,我想禁用更新按钮。 之后,如果用户更改了任何文本,则应该启用更新按钮。 我正在尝试这个,但它不起作用。 我的所有文本框autopost属性都为true。 我可以禁用,但如果用户开始在文本框中输入新文本,我将无法再次启用它。 我正在使用textbox.textchanged事件来实现这一点。

Jquery方法:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
    </div>
    </form>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $("#<%= TextBox1.ClientID %>").keyup(function () {
                $("#<%= Button1.ClientID %>").attr("disabled", $(this).val() == "");
        });
    </script>


</body>

或者您可以在服务器端执行此操作:

<asp:textbox id="TextBox1" runat="server" autopostback="True" ontextchanged="TextBox1_TextChanged"></asp:textbox>
<asp:button id="Button1" runat="server" enabled="False" text="Button" />

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Button1.Enabled = !String.IsNullOrEmpty(TextBox1.Text);
}

@Hoangnnm解决方案看起来不错。 只需包含后来的jQuery Lib,例如版本1.9.0而不是1.4.1,这是相当过时的:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>

暂无
暂无

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

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