您现在的位置是:网站首页> 编程资料编程资料
asp.net表单提交时防重复提交并执行前台的JS验证_实用技巧_
2023-05-25
400人已围观
简介 asp.net表单提交时防重复提交并执行前台的JS验证_实用技巧_
在项目开发中,遇到这样的一个情况,就是用户重复提交。当然这个不能怪用户,只能怪.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 = $("
其中在JS 验证失败中将
ControlRegPostResult = false;
这样基本上满足我的目的了。
ButtonLoading.gif 可以是一个打转的图片 ,也可以和按钮一样大。反正目的是这个层把按钮遮住。
在网上搜了一下,解决方案是不少,比如:
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 = $("
");
btnDiv.attr("id", "Mask_BTN");
var divimg = $("
");
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;
}
}
btnDiv.attr("id", "Mask_BTN");
var divimg = $("
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 可以是一个打转的图片 ,也可以和按钮一样大。反正目的是这个层把按钮遮住。
您可能感兴趣的文章:
相关内容
- asp.net后台关闭当前页面并传值的两种方法_实用技巧_
- C#判断文件路径是否存在或者判断文件是否存在的方法_实用技巧_
- ASP.NET中在不同的子域中共享Session的具体方法_实用技巧_
- 将选择的图片显示在listview中,并显示filename,path和type的简单实例_实用技巧_
- 为每个页面加上Session判断的小例子_实用技巧_
- C#和asp.net中链接数据库中参数的几种传递方法实例代码_实用技巧_
- GridView中checkbox"全选/取消"完美兼容IE和Firefox_实用技巧_
- 使用DataTable.Select 方法时,特殊字符的转义方法分享_实用技巧_
- ASP.NET ASHX中获得Session的方法_实用技巧_
- ASP.NET 实现验证码以及刷新验证码的小例子_实用技巧_
点击排行
本栏推荐
