0

我有带有TextBox以下内容的网络表单Validator

<script src="../script/jquery-2.0.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("form").submit(function () {
            $('input[type=submit]', this).attr('disabled', 'disabled');
        });
    });
</script>
<form id="form1" runat="server">
    <asp:RequiredFieldValidator ID="v" runat="server" ControlToValidate="name" ErrorMessage="Required"></asp:RequiredFieldValidator>
    <asp:TextBox ID="name" runat="server"></asp:TextBox>
    <asp:Button ID="submit" runat="server" Text="Go" CausesValidation="true" />
</form>

我想在提交button表单时禁用提交,但是此代码在无效时也禁用提交(button为空)。TextBoxValidator

我想要一种适用于所有 ASP.NET 验证器的方法,而不仅仅是RequiredFieldValidator.

如何button仅在实际提交中禁用提交?

4

3 回答 3

1

在禁用提交按钮之前检查全局Page_IsValid变量是否设置为:true

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("form").submit(function () {
                if (Page_IsValid)
                    $('input[type=submit]', this).prop('disabled', true);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:RequiredFieldValidator ID="v" runat="server" ControlToValidate="name" ErrorMessage="Required"></asp:RequiredFieldValidator>
        <asp:TextBox ID="name" runat="server"></asp:TextBox>
        <asp:Button ID="submit" runat="server" Text="Go" CausesValidation="true" />
    </form>
</body>
</html>
于 2013-09-13T16:52:57.173 回答
0
protected void submit_Click(object sender, EventArgs e) {
    if (name.Text != String.Empty) {
        submit.Enabled = false;
    } else {
        submit.Enabled = true;
    }
}

此代码没有 java 脚本

于 2013-09-11T16:13:13.803 回答
0
$(document).ready(function () {
        $("form").submit(function (e) {
         if($.trim($('#<%=name.ClientID%>').val()) !='' ){ //if textbox not empty
            $('input[type=submit]', this).attr('disabled', 'disabled');
         }else{
            e.preventDefault();
         }
        });
    });

event.preventDefault

你可以使用.prop()

 $('input[type=submit]', this).prop('disabled', true);
于 2013-09-11T16:10:18.840 回答