简体   繁体   中英

Check if Element is last element in Selection

How can I check if the current element in a .each loop is the last element in a selection?

I have tried it with the following code:

$('#order').find('.xyz').find('input[id ^="order"]').not('[id $="_zyx"]').each(function (index, element) {
    var isLast = ($(element) == $('#order').find('.xyz').find('input[id ^="order"]').not('[id $="_zyx"]').last());
});

With the query I am getting three elements. And I want to select the last element of this selection for further use.

In order to determine which element is the right one I have tested it with the code above but I am getting false for isLast for the last element where it should be true .

Am I missing something?

You don't need to use Jquery for this. Use the length of your elements and the index of your loop.

var elements = $('#order').find('.xyz').find('input[id ^="order"]').not('[id $="_zyx"]');

elements.each(function (index, element) {
    var isLast = (index+1) === elements.length;
});

查看您的HTML会很有帮助,但是您应该只能使用最后一个选择器: https : //api.jquery.com/last-selector/

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