简体   繁体   中英

How to find an element in an array by attribute in jQuery

I need help finding an object in an array of jQuery selectors by attribute.

This is the code used for selection of the inputs elements in a table:

var tableInputs = $('#clienti-table input:not(#additionalAds)');

In variable tableInputs there are 13 input elements. I need to find each element by the id attribute. Is there any way to do it? Hope someone can help me.

Thanks in advance.

You can loop over the colleciton with .each() :

tableInputs.each(function(){
    var elem = this; //do something with this.
    var id = elem.attr('id');
});

Or you can extract an element with a particular id, like this:

var $myElem = tableInputs.find('#myId');

... or by specifying the context in which to look for your element, like this:

var $myElem = $('#myId', tableElements);

You can use a for loop:

for (var i = 0; i < tableInputs.length; i++) {
    console.log(tableInputs[i].id);
}

试试这个... $('#clienti-table input:not([id='additionalAds']));

您可以使用filter获取具有给定ID的元素。

tableInputs.filter('#'+someid);// this gives you the element in the selection with id `someid`

Please try using .find()

el = $('#clienti-table input:not(#additionalAds)').find('#id');

http://api.jquery.com/find/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM