Home > Web Front-end > JS Tutorial > Summary of usage of the method of obtaining id value in jquery

Summary of usage of the method of obtaining id value in jquery

巴扎黑
Release: 2017-06-20 15:49:29
Original
1760 people have browsed it

ID is that some elements in the html page can have a unique id for this page, including p, input, iframe, etc. Now I will introduce to you how to use jquery to get the id. Value method, friends who need to know can refer to

The code is as follows:

<p id="product_shift_out_{m}"> </p>
<script language = "
JavaScript
" type="text/javascript">
$(
document
).ready(function(){
name = $(&#39;p&#39;).eq(0).attr(&#39;id&#39;);
alert(name)
});
</script>
Copy after login

eq(0) is to take 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): Count from 0

Example
Find the second line

HTML code:

The code is as follows :

<table>
<tr><td>Header 1</td></tr>
<tr><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
Copy after login

jQuery code:

The code is as follows:

$("tr:eq(1)")

Result:

The code is as follows:

[Value 1 ]

Get the values ​​of different ids

The code is as follows:

<script src="js/jquery.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){
var len = $("#group span").size();//获取span标签的个数
var arr = [];
for(var index = 0; index < len-1; index++){//创建一个数字
数组
arr[index] = index;
}
$.each(arr, function(i){//
循环
得到不同的id的值
var idValue = $("#group span").eq(i).attr("id");
if(idValue != &#39;&#39;){
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>
Copy after login

This will get all the ids you want:
0_1
0_2
0_3
0_4
0_5

Text box, text area:

The code is as follows:

$("#txt").attr("value",&#39;&#39;);//清空内容
$("#txt").attr("value",&#39;11&#39;);//填充内容
Copy after login

Multiple selection box checkbox:

The code is as follows:

$("#chk1").attr("checked",&#39;&#39;);//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr(&#39;checked&#39;)==undefined) //判断是否已经打勾
Copy after login

The above is the detailed content of Summary of usage of the method of obtaining id value in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template