在表單送出的時候,使用者有時候會重複的點選送出按鈕,會導緻多次送出,産生重複資料。我們應該怎麼辦了?一般想法是:通過attachevent在 form的onsubmit事件中寫一個方法,每次觸發該事件時執行該方法,我們可以給form增加一個submited屬性,每次判斷這個屬性,為 false的時候繼續送出表單并且設定form.submited=true,反之說明表單已經送出,就不再送出表單。
但是,出現問題了:送出表單一般有三種方式
<form action=xxx.aspx onsubmit="return false;"name=form1>
1.<input type=submit >submit按鈕
2.<input type=text>當form中隻有一個文本本框按回車的時候
3.<input type=button onclick=javascript: form1.submit()>自己寫腳本送出
</form>
前兩種是可以觸發 onsubmit事件的,但是第三種不會。是以,僅僅捕獲onsubmit事件時不行的,還必須在form的submit方法執行前,判斷表單是否在送出。
function $setformchecksubmited(){
var frms=document.forms;
for(var i=0;i<document.forms.length;i++)
{
frms[i].basesubmit=frms[i].submit;//儲存表單原來的submit 方法
frms[i].submited=false;//添加一個submited屬性,并且設定其為false
frms[i].submit=new function("$submitform(this)"); $addelementeventhandler(frms[i],"onsubmit","$submitform(document.forms["+i+"])");
}
}
//送出一個表單,如果目前表單已經送出,按麼就不會繼續送出該表單
function $submitform(frm)
{
if (frm.submited) return false;
frm.submited=true;
frm.basesubmit();
}
//下面是addelementeventhandler的實作
/*
添加一個方法到到一個對象的一個的一個事件中
element 要設定的對象
eventname 事件名稱, 字元串類型的。
methodname表示函數名稱,字元串類型的。
*/
function $addelementeventhandler(element,eventname,methodname)
if (document.all)
{
element.attachevent(eventname,new function(methodname));
}
else
if (eventname.substring(0,2)=="on") eventname=eventname.substring(2);//firefox中所有的事件名稱前面是沒有on的。
element.addeventlistener(eventname,new function(methodname));
執行個體代碼如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>無标題文檔</title>
<script language='javascript'>
function checkusername(){
if (document.form1.username.value == '' ){
alert('請輸入要注冊的會員!');
document.form1.username.focus();
return false;
}
document.form1.nextstep.disabled=true;
document.form1.nextstep.value='請稍後...'
return true;
</script>
</head>
<body>
<!--注冊-->
<a name="top"></a>
<h5 id="reg_title">新使用者注冊</h5>
<form action="/reg/registernew.dll" method="post" name="form1" onsubmit="return checkusername();">
<input id=hddinfo2 name=hddinfo type=hidden value=1996021993>
<input id="ssver2" name="ssver" type="hidden" value="">
<input id=urls name=urls type=hidden value="http://reg.ssreader.com/newuser.asp">
<input id=urld name=urld type=hidden value="http://reg.ssreader.com/lineuser.asp">
<input id=proc2 name=proc type=hidden value=1>
<div id="reg_userland_licence" class="reg_userland_info">
<div class="reg_user_title" id="r_user">第二步:設定使用者名 </div>
<p>* 使用者名:
<input name="username" id="usname" type="text" size="16" maxlength="16" />
</p>
<div style="width: 100%; text-align: center; margin: 25px 0 0 0;">
<input name="nextstep" id="nextstep" type="submit" value="下一步" /></div>
</div>
</body>
</html>
另一種方法如下:
<doctype html>
<html>
<body bgcolor="#ffffff">
<form name='formsubmitf' id ="the" method="post" action="xxx.asp">
<input type='hidden' name='mypretime' value='0'>
<input type="button" value="寫好了" name="button1" class="4round" onclick='formsubmit()'>
function formsubmit() {
today = new date();
var nowhour = today.gethours();
var nowminute = today.getminutes();
var nowsecond = today.getseconds();
var mysec = (nowhour*3600)+(nowminute*60)+nowsecond;
if((mysec-document.formsubmitf.mypretime.value)>600){
//600隻是一個時間值,就是5分鐘内禁止重複送出,值随便設
document.formsubmitf.mypretime.value=mysec;
}
else{
alert(' 按一次就夠了,請勿重複送出!請耐心等待!謝謝合作!');
return false;
document.forms.formsubmitf.submit();
不過這個有個缺點,就是重新整理一次,檢測就不起作用,好處就是利用js檢測,不需要額外的權限支援。
當然方法還有比如讓後退失效,讓按鈕失效顯示“等待。。。”等等