簡體   English   中英

如何在jQuery中按屬性查找數組中的元素

[英]How to find an element in an array by attribute in jQuery

我需要按屬性在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);

您可以使用for循環:

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`

請嘗試使用.find()

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM