本文章為你提供一款防止重複提交網頁特效代碼哦,可以防止使用者不停的點擊去提交表單,我們這裡會只提交一次哦。
<html xmlns="HTTP://www.jzread.com/1999/xhtml">
<head runat="server">
<script type="text/網頁特效">
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))//小於1秒則認為重複提交
{
event.cancelbubble = true; 測試時發現,如果直接單獨設置cancelbubble、或者returnvalue來取消事件,經常取消不了,依然存在重複提交的可能。 因此只好用傳回值來實現手工控制!
return false;
}
if (typeof (page_clientvalidate) == 'function'
&& ((validategroup == undefined && page_clientvalidate() == false)
|| (validategroup != undefined && page_clientvalidate(validategroup) == false)))
{ //如果指定了validategroup,則只驗證改組;如果未指定,則驗證全部驗證控制項
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&g t;
<asp教程: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="點擊一次,類比連續點擊'按我1'兩次" />
</form>
</body>
</html>