防止重複提交js代碼

來源:互聯網
上載者:User
關鍵字 網頁製作 Ajax JavaScript

本文章為你提供一款防止重複提交網頁特效代碼哦,可以防止使用者不停的點擊去提交表單,我們這裡會只提交一次哦。

<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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.