AJAX prevents repeated request submission.

Source: Internet
Author: User

When AJAX (jQuery) is used for asynchronous data request, to prevent users from repeatedly clicking the button for some reason, we need to disable the request submission button.

Key Points: The attr and removeAttr functions of jQuery are mainly the disabled attribute of the element button.
Demo:
Copy codeThe Code is as follows:
<Body>
<A class = "disabled"> Button disabeld </a> <a class = "abled"> Button abled </a> <br/>
<Input type = "button" id = "submit" name = "submit" value = "submit">
</Body>
<Script type = "text/javascript" src = "../jquery-1.2.6.js"> </script>
<Script type = "text/javascript">
$ (Function (){
// Invalidate it
$ (". Disabled"). click (function (){
$ ("# Submit"). attr ("disabled", "disabled ");
$ ("# Submit"). val ('Disabled ...');
});
// Activate
$ (". Abled"). click (function (){
$ ("# Submit"). removeAttr ("disabled ");
$ ("# Submit"). val ('submit ');
});
// Operation Request
$ ("# Submit"). click (function (){
$ ("# Submit"). attr ("disabled", "disabled ");
Alert ("hi"); // send ajax request
$ ("# Submit"). removeAttr ("disabled ");
});
});
</Script>

Description: After you click "Button disabeld", use $ ("# submit "). the attr ("disabled", "disabled") Statement disables the test Button. In essence, it adds a disabled attribute to the test Button and then clicks "Button abled" or executes an ajax () request, when the request is complete, use $ ("# submit "). removeAttr ("disabled"); Removes the "Disable" button attribute to enable the "Disable" button to avoid repeated requests.

Related Article

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.