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

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

巴扎黑
巴扎黑Original
2017-06-20 15:49:291698browse

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>

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>

jQuery code:

The code is as follows:

$("tr:eq(1)")

Result:

The code is as follows:

[a34de1251f0d9fe1e645927f19a896e8b6c5a531a458a2e790c1fd6421739d1cValue 1b90dd5946f0946207856a8a37f441edffd273fcf5bcad3dfdad3c41bd81ad3e5 ]

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>

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;);//填充内容

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) //判断是否已经打勾

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!

Statement:
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