如何在jQuery中通过属性查找数组中的元素

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

本文关键字:查找 数组 元素 属性 jQuery      更新时间:2023-09-26

我需要帮助在jQuery选择器数组中找到一个对象的属性。

这是用于选择表中输入元素的代码:

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

变量tableInputs有13个输入元素。我需要通过id属性找到每个元素。有什么办法吗?希望有人能帮助我。

您可以使用.each():

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

或者您可以提取具有特定id的元素,如下所示:

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

…或者指定查找元素的上下文,如下所示:

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

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

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

你可以使用for循环:

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

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

请尝试使用。find()

el = $('#clienti-table input:not(#additionalAds)').find('#id');
http://api.jquery.com/find/