When jQuery sets the radio value, use eq to replace nth

Source: Internet
Author: User

This article introduces how to replace nth with eq when jQuery sets the radio value. I hope this article will help you.

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.

Use 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.