使用名称jquery通过索引获取id值 [英] get id value by index using name jquery

查看:97
本文介绍了使用名称jquery通过索引获取id值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < input id =1name =myTexttype = textvalue =20/> 
< input id =2name =myTexttype =textvalue =30/>
< input id =3name =myTexttype =textvalue =40/>

如何通过 index获取id值使用名字?



以下代码片段无效

  var getVal = $ ( '[名称= 会将myText]')指数(1)。 


解决方案

jQuery拥有像数组一样的DOM元素所以你可以使用索引操作符( [] )来获取元素,或者使用获取包含所需元素的jQuery对象:eq(n )`.eq(n)`

  $('input [name =myText] :eq(1)')。attr('id')

c> index(1)第一或第二:

  $ ('input [name =myText]:eq(0)')。attr('id')//首先
$('input [name =myText]:eq(1)')。 attr('id')//第二个

或者:

  $('input [name =myText]')[0] .id //首先


html

<input id="1" name="myText" type="text" value="20"/>
<input id="2" name="myText" type="text" value="30"/>
<input id="3" name="myText" type="text" value="40"/>

How can I get id value by index using name?

The following code snippet is not working

var getVal = $('[name="myText"]').index(1);

解决方案

jQuery holds the DOM elements in the set like an array so you can use the indexes operator([]) to get the element, or get the jQuery object that wraps the desired element with :eq(n) `.eq(n)`

$('input[name="myText"]:eq(1)').attr('id')

You should mention what to you consider to be index(1) the first or the second:

$('input[name="myText"]:eq(0)').attr('id') // First
$('input[name="myText"]:eq(1)').attr('id') // Second

Or:

$('input[name="myText"]')[0].id // First

这篇关于使用名称jquery通过索引获取id值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆