使用名称jquery通过索引获取id值

时间:2022-05-12 09:18:31

html

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?

如何使用名称通过索引获得id值?

The following code snippet is not working

下面的代码片段不起作用。

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

3 个解决方案

#1


10  

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)`

jQuery将DOM元素像数组一样保存在集合中,这样您就可以使用索引操作符([])来获取元素,或者获得用eq(n)包装所需元素的jQuery对象“.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:

你应该提及你认为的索引(1)第一个或第二个:

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

Or:

或者:

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

#2


4  

If you want the first value, you can filter and use the attr method to get the value of the id attribute.

如果您想要第一个值,您可以过滤并使用attr方法来获取id属性的值。

var getVal = $('[name="myText"]:first').attr('id'); // first id

If you want some other element, you can use eq and choose the zero-based element in the collection.

如果需要其他元素,可以使用eq并在集合中选择从零开始的元素。

var getVal = $('[name="myText"]:eq(1)').attr('id'); // second id

#3


2  

My answer refers to accessing elements in the jQuery result object by index. You can use selectors such as :eq indicated in other answers.

我的答案是通过索引访问jQuery结果对象中的元素。您可以使用选择器,如:eq指示在其他答案。

However, you can use .get(1) instead of your index.

但是,可以使用.get(1)代替索引。

var id = $('[name="myText"]').get(1).id;

Is equivalent to

相当于

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

Example: http://jsfiddle.net/HackedByChinese/UmKw6/1/

例如:http://jsfiddle.net/HackedByChinese/UmKw6/1/

The second method is the preferred route, since it means you never leave the jQuery result object and thus can chain other jQuery calls in one statement.

第二个方法是首选路径,因为它意味着您永远不会离开jQuery result对象,因此可以在一条语句中链接其他jQuery调用。

var id = $('[name="myText"]:eq(1)').css('color', 'red').attr('id'); // example of chaining jQuery methods. sets the text color to red and then returns the id.

#1


10  

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)`

jQuery将DOM元素像数组一样保存在集合中,这样您就可以使用索引操作符([])来获取元素,或者获得用eq(n)包装所需元素的jQuery对象“.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:

你应该提及你认为的索引(1)第一个或第二个:

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

Or:

或者:

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

#2


4  

If you want the first value, you can filter and use the attr method to get the value of the id attribute.

如果您想要第一个值,您可以过滤并使用attr方法来获取id属性的值。

var getVal = $('[name="myText"]:first').attr('id'); // first id

If you want some other element, you can use eq and choose the zero-based element in the collection.

如果需要其他元素,可以使用eq并在集合中选择从零开始的元素。

var getVal = $('[name="myText"]:eq(1)').attr('id'); // second id

#3


2  

My answer refers to accessing elements in the jQuery result object by index. You can use selectors such as :eq indicated in other answers.

我的答案是通过索引访问jQuery结果对象中的元素。您可以使用选择器,如:eq指示在其他答案。

However, you can use .get(1) instead of your index.

但是,可以使用.get(1)代替索引。

var id = $('[name="myText"]').get(1).id;

Is equivalent to

相当于

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

Example: http://jsfiddle.net/HackedByChinese/UmKw6/1/

例如:http://jsfiddle.net/HackedByChinese/UmKw6/1/

The second method is the preferred route, since it means you never leave the jQuery result object and thus can chain other jQuery calls in one statement.

第二个方法是首选路径,因为它意味着您永远不会离开jQuery result对象,因此可以在一条语句中链接其他jQuery调用。

var id = $('[name="myText"]:eq(1)').css('color', 'red').attr('id'); // example of chaining jQuery methods. sets the text color to red and then returns the id.