Example of setting radio value for eq in jQuery

Source: Internet
Author: User

I have never understood eq in jquery, but I found that eq is used in many places. I will share with you several examples below.

Definition and usage

: The eq () selector selects an element with the specified index value.

The index value starts from 0, and the index value of all the first element is 0 (not 1 ).

It is often used with other elements/selectors to select elements with specific sequence numbers in the specified group (in the preceding example ).

The Code is as follows: Copy code

<P>
<Label for = ""> Gender: </label>
<Span>
<Em> male </em>
<Input type = "radio" name = "sex" value = "male"/>
</Span>
<Span>
<Em> female </em>
<Input type = "radio" name = "sex" value = "female"/>
</Span>
</P>

When jQuery sets the radio value, we can see that someone uses the nth method.

? Use nth

The Code is as follows: Copy code

$ ('Input: radio [name = sex]: nth (0) '). attr ('checked', true );

In fact, nth and eq are the same and can be replaced with eq.

The Code is as follows: Copy code

Var sex = 'female ';
If (sex = 'male '){
$ ("Input: radio [name = 'sex ']: eq (0)"). attr ('checked', true );
} Else {
$ ("Input: radio [name = 'sex ']: eq (1)"). attr ('checked', true );
}

In jQuery source code, we can see that nth is not recommended for use. If used, it will be converted to eq.

The Code is as follows: Copy code

// Deprecated

Expr. pseudo dos ["nth"] = Expr. pseudo dos ["eq"];

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.