JQuery operation single-choice button and check button example

Source: Internet
Author: User

The single button and the check button are frequently used during development. Next I will use JQuery to operate the single button and check button:

Single button:

There are three ways to obtain a single-choice button object through JQuery:

① ID: $ ("# radioId ")

② NAME: $ (": input [name = 'radioname']")

③ TYPE: $ ("input [type = radio]"), which may be written on some materials: $ ("input [@ type = radio]" "), this is related to your JQuery version. If it is an old version, you can use the new version. If you do not know which version is suitable for use, you can try it one by one, it can be done in one attempt, and you can learn more. Why not!

We all know that only one of the single-choice buttons can take effect. To achieve this effect, many people may think that you can change the ID to the same one. In fact, after you try it, you will know that you can still select multiple radio buttons in a group. Because the key attribute to achieve this effect is not ID, but NAME.

After the introduction, let's put it into use:

After obtaining the single-choice button object, we have to traverse the object, because the object type is an array, and we need to judge whether it is selected one by one, then retrieve the required value from the selected button. The sample code is as follows:
Copy codeThe Code is as follows:
Var itemradio = $ ("input [type = radio]");
Result = "";
Nums = itemradio. length;
For (I = 0; I <nums; I ++ ){
If (itemradio [I]. checked ){
Result = itemradio [I]. value + "," + itemradio [I]. id + ";" + result;
}
}

The result is the value I want to obtain. you may understand the previous value, and some people in the latter ID may be confused. Here, I will make an extension: all objects in JS can directly obtain the attribute value through.

Check box:

For the check box, all we need is the effect of "select all" and "reselect". If JQuery is used, we can do it in just one sentence, as shown below:
Copy codeThe Code is as follows:
Var checkedObj = $ ('input: checkbox [name = "checkbox"]: checked ');
Var values = "";
CheckedObj. each (function (){
Var value = this. value + ",";
Values + = value;
});

Okay, you can use the above two sections of code to operate the check box and single-choice button!

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.