JQuery prevents repeated click events from submitting instances

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.