2012-07-28 217 views
5

所以我必須提交按鈕,看起來像這樣:的JavaScript/jQuery的禁止提交點擊按鈕,防止重複提交

<a href="#" id="submitbutton" 
onClick="document.getElementById('UploaderSWF').submit();"> 
<img src="../images/user/create-product.png" border="0" /></a> 

當我雙擊它雙重的提交顯然,問題是, 我m將信息保存在數據庫中,所以我將在那裏發佈dublicate信息, ,我不想那樣。這位上傳使用閃存和javscript這裏是代碼的一小片 ,是有關提交的東西(如果它幫助)

$.fn.agileUploaderSubmit = function() { 
    if($.browser.msie && $.browser.version == '6.0') { 
     window.document.agileUploaderSWF.submit(); 
    } else { 
     document.getElementById('agileUploaderSWF').submit(); 
     return false; 
    } 
} 

謝謝你們。 我感謝您的幫助。這真的是我無法自己做的事 ,因爲我有這樣一個與js的小經驗,我真的不知道如何 做東西。 謝謝。

回答

8

試試這個剪斷:

$('#your_submit_id').click(function(){ 
    $(this).attr('disabled'); 
}); 

編輯1

哦,你的情況這是一個鏈接,並沒有提交按鈕......

var submitted = false; 

$.fn.agileUploaderSubmit = function() { 
    if (false == submitted) 
    { 
     submitted = true; 

     if($.browser.msie && $.browser.version == '6.0') { 
      window.document.agileUploaderSWF.submit(); 
     } else { 
      document.getElementById('agileUploaderSWF').submit(); 
     } 
    } 

    return false; 
} 

編輯2

爲了簡化這個,試試這個:

<!doctype html> 

<html dir="ltr" lang="en"> 

<head> 

<meta charset="utf-8" /> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<script type="text/javascript"> 
<!--//--><![CDATA[//><!-- 
    $(document).ready(function() 
    { 
     $('#yourSubmitId').click(function() 
     { 
      $(this).attr('disabled',true); 

      /* your submit stuff here */ 

      return false; 
     }); 
    }); 
//--><!]]> 
</script> 

</head> 
<body> 

<form id="yourFormId" name="yourFormId" method="post" action="#"> 
    <input type="image" id="yourSubmitId" name="yourSubmitId" src="yourImage.png" alt="Submit" /> 
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

</body> 
</html> 

使用表單元素,如<input type="image" />,提交表單不是一個正常的鏈接。

這工作正常!

看看jQuery.post()提交您的表格。

祝你好運。

編輯3

這很適合我太:

<!doctype html> 

<html dir="ltr" lang="en"> 

<head> 

<meta charset="utf-8" /> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

<script type="text/javascript"> 
<!--//--><![CDATA[//><!-- 
    $(document).ready(function() 
    { 
     var agileUploaderSWFsubmitted = false; 

     $('#submitbutton').click(function() 
     { 
      if (false == agileUploaderSWFsubmitted) 
      { 
       agileUploaderSWFsubmitted = true; 

       //console.log('click event triggered'); 

       if ($.browser.msie && $.browser.version == '6.0') 
       { 
        window.document.agileUploaderSWF.submit(); 
       } 
       else 
       { 
        document.getElementById('agileUploaderSWF').submit(); 
       } 
      } 

      return false; 
     }); 
    }); 
//--><!]]> 
</script> 

</head> 
<body> 

<form id="agileUploaderSWF" name="agileUploaderSWF" method="post" action="http://your.action/script.php"> 
    <input type="text" id="agileUploaderSWF_text" name="agileUploaderSWF_text" /> 
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

<a href="#" id="submitbutton"><img src="../images/user/create-product.png" border="0" /></a>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

</body> 
</html> 

希望這有助於。

+0

謝謝,我會嘗試。 – inrob 2012-07-28 23:09:39

+0

你好Raisch。感謝您的解決方案。我申請你的第二個代碼 ,它沒有真正的工作。仍然提交多次。我是否也應用第一塊代碼? – inrob 2012-07-28 23:37:27

+0

@bornie - 沒問題,所以你可以接受這個答案。 :-) – Raisch 2012-07-28 23:40:49

4

添加以下到onclick

onclick="document.getElementById('submitbutton').disabled = true;document.getElementById('UploaderSWF').submit();" 

這就是說,你將不得不處理這雙在服務器端提交的預防也。

+0

謝謝,我會嘗試。服務器端沒有問題。我可以做到這一點,我知道我不能僅僅依靠js,因爲有時用戶在其瀏覽器上禁用了 javascript支持。 – inrob 2012-07-28 23:09:08

+0

該用戶正在使用jquery ..我沒有看到需要onClick屬性或document.getElementById。 – Daedalus 2012-07-28 23:10:13

+0

@bornie,我做了一個修改。它應該是'document.getElementById(''**'submitbutton' **'').disabled' – Nivas 2012-07-28 23:12:43

-1

這將禁用所有提交按鈕和鏈接與類提交,或者點擊一個表單中的屬性數據提交:

$(document).ready(function() { 
    $('form').submit(function() { 
     $(this).find('input[type="submit"]').attr('disabled', true); 
     $(this).find('a[data-submit], a.submit').click(function() { 
      return false; 
     }); 
    } 
}); 

這將自動適用於所有形式的方式。如果你只是想要一個,使用id而不是形式。不過,你可能已經知道了。

+0

那麼問題是,如果你可以看得更仔細,提交按鈕不在

。它是分開的。你認爲你的代碼仍然有效? – inrob 2012-07-29 15:21:39

相關問題