javascript 防止重复提交

很多时候我们都需要防止重复提交,这方面的文章也比较多,实现的途径差别也很大.以下是我写的一种控制提交的方式.因为有些时候即使服务器能够识别重复的提交,也会造成问题.比如需要很长等待时间的操作,在首次提交后,不断重复提交,页面可能会死掉.用脚本来控制的话可以防止这种问题.当然也可以脚本和服务器都进行控制,这样就比较完美了.
    闲话少说,将以下脚本放置于页面顶部.

 1 None.gif document.IsPosted  =   false ;
 2 None.gif function  CancelDubSubmit()
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif
 4InBlock.gif    if ((typeof(event.returnValue) == "undefined" || event.returnValue== true&& !document.IsPosted)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
 6InBlock.gif        document.IsPosted = true;
 7InBlock.gif        event.returnValue = true;
 8ExpandedSubBlockEnd.gif    }

 9InBlock.gif    else
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{          
11InBlock.gif        event.returnValue = false;
12ExpandedSubBlockEnd.gif    }

13ExpandedBlockEnd.gif}

以下加粗部分放置于form标签中,如果你已经有了onsubmit事件的其他执行函数,可以放在一起,最好将CancelDubSubmit()函数放在最后.
None.gif < form  id ="Form1"  onsubmit ="CancelDubSubmit();"  method ="post"  runat ="server" >

    其中document.IsPosted是为了记录是否回送.一旦页面回送,document.IsPosted将为true. 重新加载后,document.IsPosted=false将被执行.当onsubmit事件没有其他执行函数或者其他执行函数返回true并且document.IsPosted=false时,回送页面,否则停止回送.
    以上方法不能控制使用 function __doPostBack() 函数的服务器端控件.因为在此函数中的提交是靠 document.Form1.submit() 实现的,它不会触发Onsubmit事件.那么我们还需要重写__doPostBack() 函数.
ExpandedBlockStart.gif ContractedBlock.gif function  __doPostBack(eventTarget, eventArgument)  dot.gif {
InBlock.gif    
if (!document.IsPosted)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
var theform;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (window.navigator.appName.toLowerCase().indexOf("netscape"> -1dot.gif{
InBlock.gif            theform 
= document.forms["Form1"];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
else dot.gif{
InBlock.gif            theform 
= document.Form1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        theform.__EVENTTARGET.value 
= eventTarget.split("$").join(":");
InBlock.gif        theform.__EVENTARGUMENT.value 
= eventArgument;    
InBlock.gif        
InBlock.gif        document.IsPosted 
= true;
InBlock.gif        theform.submit();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

请将以上代码放置于页面的原__doPostBack() 函数之后.

转载于:https://www.cnblogs.com/redfire0922/archive/2006/06/20/430746.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值