asp.net表單提交時防重複提交並執行前台的JS驗證_實用技巧

來源:互聯網
上載者:User
在項目開發中,遇到這樣的一個情況,就是使用者重複提交。當然這個不能怪使用者,只能怪.NET或者伺服器反應遲鈍......我是這樣理解的。
在網上搜了一下,解決方案是不少,比如:
http://bbs.csdn.net/topics/340048988
(這個大家提了不少建議)
http://www.cnblogs.com/blsong/archive/2009/12/24/1631144.html
(這個基本上總結了網上的方法)
但實際上做互連網web項目中,需要在前台執行JS或者Jquery的驗證(主要是增強使用者體驗),那麼再使用上面的方法,就會出現問題。要麼重複提交依然存在,要麼前台JS驗證失效。最後沒辦法,只有自己寫一個,在滿足阻止使用者重複提交的情況下,還能保證前台JS驗證有效。代碼如下:
複製代碼 代碼如下:

//按鈕註冊載入樣式事件
var ItSelfButton;
var ControlRegPostResult = true;
function AddInputClick() {
$("input[type='submit']").click(function () {
ItSelfButton = $(this);
if (ItSelfButton.attr("repeat") == null) {
var btnDiv = $("<div>");
btnDiv.attr("id", "Mask_BTN");
var divimg = $("<img>");
divimg.attr("alt", "載入中...");
divimg.attr("src", "/Images/ButtonLoading.gif");
divimg.css({ "margin-left": ($(this).width() - 4) / 2, "margin-top": ($(this).height() - 16) / 2 });
btnDiv.append(divimg);
btnDiv.css({ width: $(this).width() + 12 + "px", height: $(this).height() + "px", top: $(this).offset().top + "px", left: $(this).offset().left + "px", position: "absolute" });
$(document.body).append(btnDiv);
setTimeout(MaskTimeOutRemove, 200);
}
});
}
$(function () {
AddInputClick();
});
$(window).resize(function () {
if (ItSelfButton != null) {
$("#Mask_BTN").css({ top: ItSelfButton.offset().top + "px", left: ItSelfButton.offset().left + "px" });
}
});
function MaskRemove() {
$("#Mask_BTN").remove();
}
function MaskTimeOutRemove() {
if (!ControlRegPostResult) {
$("#Mask_BTN").remove();
ControlRegPostResult = true;
}
}

其中在JS 驗證失敗中將
複製代碼 代碼如下:

ControlRegPostResult = false;

這樣基本上滿足我的目的了。
ButtonLoading.gif 可以是一個打轉的圖片 ,也可以和按鈕一樣大。反正目的是這個層把按鈕遮住。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.