Prevent duplicate submission of JS code

Source: Internet
Author: User
  code is as follows copy code

<script type= "text/web Effects" >
var date = new Array ();
function Disablebutton (button, Validategroup)
{
Date.push (new Date ());
if (Date.length > 1
&& (Date[date.length-1].gettime ()-Date[date.length-2].gettime () < 1000)//less than 1 seconds is considered duplicate commit
{
Event.cancelbubble = true; Test found that if directly set cancelbubble, or ReturnValue to cancel the event, often canceled, there is still the possibility of duplicate submissions. So we have to use the return value to achieve manual control!
return false;
}
if (typeof (page_clientvalidate) = = ' function '
&& ((Validategroup = = undefined && page_clientvalidate () = False)
|| (Validategroup!= undefined && page_clientvalidate (validategroup) = False))
{//If Validategroup is specified, only the reorganization is validated; if not specified, validate all validation controls
return false;
}

Button.disabled = true;
return true;
}

function DoubleClick ()
{
var button1 = document.getElementById (' Btnpostback1 ');
Button1.onclick ();
Button1.onclick ();
}
</script>
<body>
<form id= "form" runat= "Server" >
<div>
<asp Tutorial: TextBox runat= "Server" id= "TBXINPUT1" ></asp:textbox>
<asp:requiredfieldvalidator id= "Requiredfieldvalidator1" runat= "Server" controltovalidate= "TBXINPUT1"
validationgroup= "group1" errormessage= "*" ></asp:requiredfieldvalidator>
<asp:button runat= "Server" id= "Btnpostback1" text= "Press me 1" usesubmitbehavior= "false"
validationgroup= "Group1" onclientclick= "If" (!disablebutton (This, ' group1 ')) return false; "onclick=" Btnpostback_ Click ">
</asp:button>
</div>
<div>
<asp:textbox runat= "Server" id= "Tbxinput2" ></asp:textbox>
<asp:requiredfieldvalidator id= "Requiredfieldvalidator2" runat= "Server" controltovalidate= "Tbxinput2"
validationgroup= "Group2" errormessage= "*" ></asp:requiredfieldvalidator>
<asp:button runat= "Server" id= "Btnpostback2" text= "Press Me 2" usesubmitbehavior= "false"
Onclientclick= "if (!disablebutton (this)) return false;" onclick= "Btnpostback_click" >
</asp:button>
</div>
<input type= "button" onclick= "DoubleClick ();" Value= "Click once, simulate consecutive clicks ' Press me 1 ' two times '/>
</form>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.