首页 > 语言 > JavaScript > 正文

一个JavaScript防止表单重复提交的实例

2024-05-06 16:09:58
字体:
来源:转载
供稿:网友
防止重复表单提交的方法有很多,本文使用JavaScript来实现防止表单重复提交,很简单,但很实用,新手朋友们不要错过
    
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  5. <title>防止重复表单提交</title> 
  6. <style> 
  7. #refresh{ 
  8. display: none; 
  9. width:200px; 
  10. height:20px; 
  11. background-color: #ff0; 
  12.  
  13.  
  14. </style> 
  15.  
  16. <script> 
  17. var inprocess = false
  18. window.onload = function(){ 
  19.  
  20. document.forms["picker"].onsubmit = validateSubmit; 
  21. document.getElementById("refresh").onclick = startOver; 
  22. function validateSubmit () { 
  23. // 防止重复的表单提交 
  24. if (inprocess) return
  25. inprocess = true
  26. console.log(inprocess); 
  27. document.getElementById("submitbutton").disabled = true
  28.  
  29. document.getElementById("refresh").style.display = "block"
  30. document.getElementById("message").innerHTML = "<p>正在processing,稍等</p>"
  31. return false
  32. function startOver(){ 
  33. inprocess = false
  34. document.getElementById("submitbutton").disabled = false
  35. document.getElementById("message").innerHTML = ""
  36. document.getElementById("refresh").style.display = "none"
  37.  
  38. </script> 
  39. </head> 
  40.  
  41. <body> 
  42.  
  43. <form id="picker" method="post" action=""
  44. group1:<input type="radio" name="group1" value="one" /> 
  45. group2:<input type="radio" name="group1" value="two" /> 
  46. group3:<input type="radio" name="group1" value="three" /><br /><br /> 
  47. Input 1: <input type="text" id="intext" /> 
  48. Input 2: <input type="text" id="intext2" /> 
  49. Input 3: <input type="text" id="intext3" /> 
  50. <input type="submit" id="submitbutton" value="send form" /> 
  51. </form> 
  52. <div id="refresh"
  53. <p>单击我</p> 
  54. </div> 
  55. <div id="message"></div> 
  56. </body> 
  57. </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选