Summary of methods for getting id values in jquery

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Div id = "product_shift_out _ {m}"> </div>
<Script language = "JavaScript" type = "text/javascript">
$ (Document). ready (function (){
Name = $ ('div '). eq (0). attr ('id ');
Alert (name)
});
</Script>

Eq (0) is the first jq element...

Eq (index)
Matches an element with a given index value.

Matches a single element by its index.
Return Value
Element

Parameters
Index (Number): starts from 0.

Example
Find the second row

HTML code:

Copy codeThe Code is as follows:
<Table>
<Tr> <td> Header 1 </td> </tr>
<Tr> <td> Value 1 </td> </tr>
<Tr> <td> Value 2 </td> </tr>
</Table>

JQuery code:

Copy codeThe Code is as follows:
$ ("Tr: eq (1 )")

Result:

Copy codeThe Code is as follows:
[<Tr> <td> Value 1 </td> </tr>]

Obtain the values of different IDs

Copy codeThe Code is as follows:

<Script src = "js/jquery. js"> </script>
<Script type = "text/javascript">
<! --
$ (Document). ready (function (){
Var len = $ ("# group span"). size (); // obtain the number of span tags
Var arr = [];
For (var index = 0; index <len-1; index ++) {// create an array of numbers
Arr [index] = index;
}
$. Each (arr, function (I) {// cyclically obtain values of different IDs
Var idValue = $ ("# group span"). eq (I). attr ("id ");
If (idValue! = ''){
Alert (idValue );
}
});
});
// -->
</Script>
<Span id = "group">
<Span id = "0_1"> aaa,
<Span group_id = "0_1" class = "icon_close"> </span>
</Span>
<Span id = "0_2"> bbb,
<Span group_id = "0_2" class = "icon_close"> </span>
</Span>
<Span id = "0_3"> ccc,
<Span group_id = "0_3" class = "icon_close"> </span>
</Span>
<Span id = "0_4"> ddd,
<Span group_id = "0_4" class = "icon_close"> </span>
</Span>
<Span id = "0_5"> eee,
<Span group_id = "0_5" class = "icon_close"> </span>
</Span>
</Span>

In this way, you will get all the IDs you want:
0_1
0_2
0_3
0_4
0_5

Text Box, text area:

Copy codeThe Code is as follows:
$ ("# Txt"). attr ("value", ''); // clear the content
$ ("# Txt"). attr ("value", '11'); // fill in the content

Multiple selection box checkbox:

Copy codeThe Code is as follows:
$ ("# Chk1"). attr ("checked", ''); // do not check
$ ("# Chk2"). attr ("checked", true); // check
If ($ ("# chk1"). attr ('checked') = undefined) // you can check whether the checked has been checked.

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.