問題描述
我有一個(gè)表單,它在單擊提交按鈕時(shí)在 DB 中插入數(shù)據(jù),但問題是當(dāng)客戶端多次單擊按鈕時(shí),它發(fā)送多個(gè)創(chuàng)建請求意味著同一數(shù)據(jù)的同一時(shí)間有多個(gè)按鈕單擊事件,但不能這樣.
I have a form which inserts data in DB on Submit button click but the problem is when client click the button multiple times its sends multiple create requests means multiple button click events for the same time of same data, which must not be.
當(dāng)客戶端第一次單擊提交按鈕時(shí),我嘗試禁用該按鈕,但此后它不會(huì)調(diào)用服務(wù)器單擊事件處理程序,也不會(huì)在禁用后觸發(fā)服務(wù)器單擊事件.
I tried to disable the button when client click the Submit button first time but after this it does not call server click event handler or not fire the server click event once it got disabled.
如何處理這個(gè)多次點(diǎn)擊問題..
How to handle this multiple click problem..
我使用以下代碼禁用按鈕
I used the following code to disable the button
<script type="text/javascript">
function checkAuth(obj)
{
if(Page_ClientValidate("ValidationGroupName"))
obj.disabled=true;
}
</script>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_click" OnClientClick="checkAuth(this)" CssClass="FormButton"
ValidationGroup="ValidationGroupName" />
推薦答案
不要禁用按鈕,只是防止第二次提交.
Do not disable the button, just prevent the second submit.
這個(gè)小腳本可以完成這項(xiàng)工作,但它假設(shè)在某個(gè)時(shí)刻有回發(fā).
this little script does the job but it assumes there is a postback at a certain moment.
var formhandler = function() {
var submit, isSubmit = false;
submit = function(){
// flop and return false once by the use of operator order.
return isSubmit != (isSubmit = true);
};
return {
submit: submit
};
}(); // <-- use direct invcation to keep the internal variables "static"
附上:
document.forms[0].onsubmit = formhandler.submit;
或
OnClientClick = "formhandler.submit()";
這篇關(guān)于多個(gè)提交按鈕點(diǎn)擊問題?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!