問題描述
我有一個表單,它在單擊提交按鈕時在 DB 中插入數據,但問題是當客戶端多次單擊按鈕時,它發送多個創建請求意味著同一數據的同一時間有多個按鈕單擊事件,但不能這樣.
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.
當客戶端第一次單擊提交按鈕時,我嘗試禁用該按鈕,但此后它不會調用服務器單擊事件處理程序,也不會在禁用后觸發服務器單擊事件.
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.
如何處理這個多次點擊問題..
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.
這個小腳本可以完成這項工作,但它假設在某個時刻有回發.
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()";
這篇關于多個提交按鈕點擊問題?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!