Prevent duplicate submission of JS code

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax asp button click code date function html

This article provides you with a prevention of repeated submission of Web page special effects code Oh, you can prevent users from constantly click to submit the form, we will only submit once oh.

<html xmlns= "http://www.jzread.com/1999/xhtml" >
<head runat= "Server" >
<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>
</head>
<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=" 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=" 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>
</html>

Related Article

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.