[英]How to prevent form double submit in form that have Validator?
I have web form that have TextBox
with Validator
: 我有具有带有Validator
TextBox
Web表单:
<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>
I want to disable submit button
when form submitted but this code also disable submit button
when TextBox
is not valid by Validator
(is empty). 我想在提交表单时禁用提交button
,但是当TextBox
由Validator
无效(为空)时,此代码也禁用提交button
。
I want a way for all ASP.NET Validators,not only RequiredFieldValidator
. 我想要一种用于所有ASP.NET验证程序的方法,不仅是RequiredFieldValidator
。
How can I disable submit button
only in real submit? 如何仅在实际提交中禁用提交button
?
Check whether the global Page_IsValid
variable is set to true
before disabling the submit button: 在禁用提交按钮之前,请检查全局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>
$(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 event.preventDefault
you can use .prop() 您可以使用.prop()
$('input[type=submit]', this).prop('disabled', true);
protected void submit_Click(object sender, EventArgs e) {
if (name.Text != String.Empty) {
submit.Enabled = false;
} else {
submit.Enabled = true;
}
}
this Code without java script 此代码不带Java脚本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.