Ajax avoids repeated submission of requests by users to implement scenarios _ajax related

Source: Internet
Author: User
When using AJAX to request data asynchronously, we need to disable the request Submission button in order to avoid the user clicking the button multiple times for some reason and submitting a duplicate request.

Focus: jquery's attr and removeattr two functions, primarily the disabled attribute of the element button.
Demo:
Copy Code code as follows:

<body>
<a class= "Disabled" >button disabeld</a> <a class= "abled" >button abled</a><br/><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 () {
Make it ineffective
$ (". Disabled"). Click (function () {
$ ("#submit"). attr ("Disabled", "disabled");
$ ("#submit"). Val (' disabled ... ');
});
Activated
$ (". abled"). Click (function () {
$ ("#submit"). Removeattr ("Disabled");
$ ("#submit"). Val (' Submit ');
});
Action Request
$ ("#submit"). Click (function () {
$ ("#submit"). attr ("Disabled", "disabled");
Alert ("HI");//send Ajax Request
$ ("#submit"). Removeattr ("Disabled");
});
});
</script>

Description: When clicking Button Disabeld, use $ ("#submit"). attr ("Disabled", "disabled") to disable the test button, in essence, add a disabled property to the Test button, and then click button Abled "or execute the Send Ajax () request, when the request is complete, use $ (" #submit "). Removeattr (" Disabled "); The statement removes the Disable button properties, thereby enabling the disabled button to avoid duplicate requests.

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.