We can use jquery to prevent repeated click events from submitting forms. After clicking submit, we can disable the button or directly prompt that the form is being submitted. After the submission is successful, we can return to the reference and reset the button.
Define a global identifier
The Code is as follows: |
Copy code |
// Code snippet Var ddClick = false; $ (". Cai"). bind ('click', function (){ If (ddClick) {return ;} Post_dig ('cai '); }); $ (". Zan"). bind ('click', function (){ If (ddClick) {return ;} Post_dig ('za '); }); Function post_dig (type ){ DdClick = true; $. Ajax ({ Type: 'post ', Url: '/scripts/dig. php ', Data: {act: 'update', type: type }, Success: function (msg ){ DdClick = false; } }); } |
Example 2: run the following code to disable the Form button after completing the Form verification of the Validation plug-in. submit the Form:
The Code is as follows: |
Copy code |
$ (Document). ready (function (){ $ ("# MyForm"). validate ({ SubmitHandler: function (form ){ $ (Form ). find (": submit "). attr ("disabled", true ). attr ("value", "Submitting... "); form. submit (); } }) }); |
Example 3. Implementation idea: Click the submit button to add the disabled attribute. Do not click.
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> $ ("Input: submit"). each (function (){ Var srcclick = $ (this). attr ("onclick "); If (typeof (srcclick) = "function "){ $ (This). click (function (){ If (srcclick ()){ Setdisabled (this ); Return true; } Return false; });} }); Function setdisabled (obj ){ SetTimeout (function () {obj. disabled = true ;},100 ); } </Script> |