Home  >  Article  >  Web Front-end  >  Summary of how to get id value in jquery

Summary of how to get id value in jquery

伊谢尔伦
伊谢尔伦Original
2017-06-19 14:57:407257browse

Some elements in the html page can have a unique id for this page. Here is a summary of how to use jquery to obtain the id value. I hope it will be helpful to everyone learning jquery.

<div id="product_shift_out_{m}"> </div>
<script language = "JavaScript" type="text/javascript">
$(document).ready(function(){
name = $(&#39;div&#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 row

Get the value of different id

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

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

Multiple selection box checkbox code:

$("#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 how to get 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