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"]; |