[When the server is overloaded, submissions will be stuttered, but when the user is operating, he will keep clicking and submitting repeatedly, which will cause greater pressure on the server. So we need to limit it]
[1] Disable Submit Button
<html> <head> <script> //Prohibit the default behavior because it is necessary to simulate the server overload, so you need to first disable the function of automatically submitting the submit button function preventDef(event){ event=event||window.event; if(event.preventDefault){ return event.preventDefault; }else{ return event.returnValue=false; } } window.onload=function(){ var form=document.getElementsByTagName('form')[0]; //Get form element var txt=form.elements['txt']; //Get the form element with name attribute txt var sub=form.elements['sub']; //Get the form element with name attribute sub form.onsubmit=function(event){ //Create a submission event, because submit is form to submit events, so if we want to prohibit form, we also need to use form to prohibit event=event || window.event; preventDef(event); //Ban disable the default submission behavior sub.disabled=true //The first is to disable the button after the user submits, and then release the button after waiting for the submission to complete setTimeout(function(){ //Simulate for 5 seconds and then submit form.submit()},5000); } } </script> </head> <body> <form id="form"> <input type="text" id="txt" name="txt"> <input type="submit" id="sub"> </form> </body></html>[The second type of creating a variable for judgment]
[1] Disable Submit Button
<html> <head> <script> //Prohibit the default behavior because it is necessary to simulate the server overload, so you need to first disable the function of automatically submitting the submit button function preventDef(event){ event=event||window.event; if(event.preventDefault){ return event.preventDefault; }else{ return event.returnValue=false; } } window.onload=function(){ var form=document.getElementsByTagName('form')[0]; //Get form element var txt=form.elements['txt']; //Get the form element with name attribute txt var sub=form.elements['sub']; //Get the form element with name attribute sub var flag=true; //Create a variable first to indicate that the user has not clicked to submit form.onsubmit=function(event){ //Create a submission event, because submit is form to submit events, so if we want to prohibit form=event || window.event; preventDef(event); //Shield the default submission behavior if(flag==true){ flag=false; //Sign up the submission button}else{ alert('Submit, please do not repeat submission'); } setTimeout(function(){ //Submit form.submit()},5000); }} </script> </head> <body> <form id="form"> <input type="text" id="txt" name="txt"> <input type="submit" id="sub"> </form> </body></html>Two ways to prohibit users from submitting multiple times in JavaScript
[When the server is overloaded, submissions will be stuttered, but when the user is operating, he will keep clicking and submitting repeatedly, which will cause greater pressure on the server. So we need to limit it]
[1] Disable Submit Button
<html> <head> <script> //Prohibit the default behavior because it is necessary to simulate the server overload, so you need to first disable the function of automatically submitting the submit button function preventDef(event){ event=event||window.event; if(event.preventDefault){ return event.preventDefault; }else{ return event.returnValue=false; } } window.onload=function(){ var form=document.getElementsByTagName('form')[0]; //Get form element var txt=form.elements['txt']; //Get the form element with name attribute txt var sub=form.elements['sub']; //Get the form element with name attribute sub form.onsubmit=function(event){ //Create a submission event, because submit is form to submit events, so if we want to prohibit form, we also need to use form to prohibit event=event || window.event; preventDef(event); //Ban disable the default submission behavior sub.disabled=true //The first is to disable the button after the user submits, and then release the button after waiting for the submission to complete setTimeout(function(){ //Simulate for 5 seconds and then submit form.submit()},5000); } } </script> </head> <body> <form id="form"> <input type="text" id="txt" name="txt"> <input type="submit" id="sub"> </form> </body></html>[The second type of creating a variable for judgment]
[1] Disable Submit Button
<html> <head> <script> //Prohibit the default behavior because it is necessary to simulate the server overload, so you need to first disable the function of automatically submitting the submit button function preventDef(event){ event=event||window.event; if(event.preventDefault){ return event.preventDefault; }else{ return event.returnValue=false; } } window.onload=function(){ var form=document.getElementsByTagName('form')[0]; //Get form element var txt=form.elements['txt']; //Get the form element with name attribute txt var sub=form.elements['sub']; //Get the form element with name attribute sub var flag=true; //Create a variable first to indicate that the user has not clicked to submit form.onsubmit=function(event){ //Create a submission event, because submit is form to submit events, so if we want to prohibit form=event || window.event; preventDef(event); //Shield the default submission behavior if(flag==true){ flag=false; //Sign up the submission button}else{ alert('Submit, please do not repeat submission'); } setTimeout(function(){ //Submit form.submit()},5000); }} </script> </head> <body> <form id="form"> <input type="text" id="txt" name="txt"> <input type="submit" id="sub"> </form> </body></html>The above is the relevant information about two methods that the editor introduced to you that JavaScript prohibits users from submitting multiple times. I hope it will be helpful to everyone!