JQuery sets the single-choice button radio selected/unavailable instance code, jqueryradio

Source: Internet
Author: User

JQuery sets the single-choice button radio selected/unavailable instance code, jqueryradio

Because this part is "jQuery daily use", it is based on specific requirements and summarizes the knowledge points needed. The Code may not be well written. It is good to achieve the goal. So let's take a look at this demand: the color block mode is based on the color mode, that is, the color block can be turned on. When the color is off, the color block is unavailable. When the color is turned on, the color block radio is available, if the color block is on when the color block is off, turn it off. This is probably the case.

Let's take a look at the demo:

Let's take a look at jQuery's operations on the single-choice button radio.

1. When the color is off, the color block is unavailable.

Two steps are required. When the page is opened, if the color is disabled, make the color block "open" unavailable:

if($("#coloroff[checked]")){//alert("Hello Nowamagic!");$("#blockon").attr("disabled", true);}

In addition, when the color switch is enabled, that is, when the switch is off, make the color block unavailable, and select the color block off:

$("#coloroff").change(function(){$("#blockon").attr("disabled", true);$("#blockoff").attr("checked",true);})

2. When the color switch is enabled, make the color block available, which is very simple:

$("#coloron").change(function(){$("#blockon").attr("disabled", false);})

All code:

$(document).ready(function(){if($("#coloroff[checked]")){//alert("Hello Nowamagic!");$("#blockon").attr("disabled", true);}$("#coloroff").change(function(){$("#blockon").attr("disabled", true);$("#blockoff").attr("checked",true);})$("#coloron").change(function(){$("#blockon").attr("disabled", false);})});

Add several knowledge points:

The checkbox and radio controls do not have the readonly attribute. You need to use the disabled attribute to switch their "available/unavailable" status.

You can use the attr () method to set radio unavailability, that is, attr ("disabled", true ).

The attr () method is also used to set the selected status of radio, attr ("checked", true ).

The above section describes the jQuery single-choice button radio selected/unavailable instance code. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.