jquery Realization Click radio button Toggle Check status effect

Source: Internet
Author: User

Effect: Select the button the first time you click the radio button, and uncheck the radio button when you tap it again.

The key is to save the radio button's original value, and the second time you click. If the original selection is canceled, otherwise selected.

Look at the code, it is implemented with jquery, although the function is small. A lot of knowledge ah .....

JS Code
  1. $ (document). Ready (function () {
  2. var old = null; //Used to save the original object
  3. $ ("input[name= ' sex ']"). each (function () {//loop-bound event
  4. if (this.checked) {
  5. Old = this ; //If the current object is selected, save the object
  6. }
  7. This.onclick = function () {
  8. if (this = = old) {//if the clicked object was originally selected, Uncheck
  9. this.checked = false;
  10. old = null;
  11. } else{
  12. Old = this ;
  13. }
  14. }
  15. });
  16. });

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.