Gets the value of the radio

Source: Internet
Author: User

As jquery has become more and more important, more and more friends are being used. In the web, because controls such as checkboxes, Radiobutton, DropDownList, and so on are used more frequently, they are related to the operation of these controls in jquery. Since the version of jquery is updated very quickly, the code has changed a lot, the following jquery code is suitable for query1.4 version above.

Radio


1. To get the selected value, three methods are available:

$ (' input:radio:checked '). Val ();

$ ("input[type= ' Radio ']:checked"). Val ();

$ ("Input[name= ' rd ']:checked"). Val ();

2. Set the first radio to the selected value:

$ (' Input:radio:first '). attr (' checked ', ' checked ');

Or

$ (' Input:radio:first '). attr (' checked ', ' true ');

Note: attr ("Checked", ' checked ') = attr ("Checked", ' true ') = attr ("Checked", true)

3. Set the last radio as the selected value:

$ (' Input:radio:last '). attr (' checked ', ' checked ');

Or

$ (' Input:radio:last '). attr (' checked ', ' true ');

4. Set any one of the radio to the selected value according to the index value:

$ (' Input:radio '). EQ (index value). attr (' checked ', ' true '); index value =0,1,2 ....

Or

$ (' Input:radio '). Slice. attr (' checked ', ' true ');

5. Set Radio to the selected value based on value

$ ("input:radio[value=http://www.2cto.com/kf/201110/' Rd2 ')"). attr (' checked ', ' true ');

Or

$ ("input[value=http://www.2cto.com/kf/201110/' Rd2 ')"). attr (' checked ', ' true ');

6. Remove Radio with value Rd2

$ ("input:radio[value=http://www.2cto.com/kf/201110/' Rd2 ')". Remove ();

7. Delete the first few radio

$ ("Input:radio"). EQ (index value). Remove (); index value =0,1,2 ....

such as removing the 3rd radio:$ ("Input:radio"). EQ (2). Remove ();

8. Traverse Radio

$ (' Input:radio '). each (function (Index,domele) {

Write code

});

DropDownList

1. Get the check:

Gets the value of the selected item:

$ (' Select#sel option:selected '). Val ();

Or

$ (' Select#sel '). Find (' option:selected '). Val ();

Gets the text value of the selected item:

$ (' select#seloption:selected '). Text ();

Or

$ (' Select#sel '). Find (' option:selected '). Text ();

2. Gets the index value of the currently selected item:

$ (' Select#sel '). Get (0). SelectedIndex;

3. Get the maximum index value for the current option:

$ (' Select#sel option:last '). attr ("index")

4. Get the length of the DropDownList:

$ (' Select#sel ') [0].options.length;

Or

$ (' Select#sel '). Get (0). Options.length;

5. Set the first option to a selected value:

$ (' Select#sel option:first '). attr (' selected ', ' true ')

Or

$ (' Select#sel ') [0].selectedindex = 0;

6. Set the last option to the selected value:

Original address: http://www.hake.cc/a/biancheng/web/js/2011/1013/27444.html

Gets the value of the radio

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.