jquery找到最后一个元素

时间:2012-12-06 09:08:15

标签: jquery html

我想要的是,当我使用最后一个列表选择菜单上的tab键时,我想得到一个警告。随着我得到的jquery,我会得到一个警报但是在每个选择菜单而不是最后一个。

HTML标记

<div id="invoiceLinesForm" style="">
<ul id="sortable" class="ui-sortable">
    <li id="headerLine">
        <div class="qty">Aantal</div>
        <div class="description">Omschrijving</div>
        <div class="price">Bedrag EUR</div>
        <div class="total">Totaal</div>
        <div class="vat">BTW</div>
        <div class="actions"></div>
        <div class="clear"></div>
    </li>

    <li id="firstLine">
        <div class="qty"><input type="text" name="invoice_line[0][qty]" value="1" class="qty_input"></div>
        <div class="description"><textarea name="invoice_line[0][description]"></textarea></div>
        <div class="price"><input type="text" name="invoice_line[0][price]" value="0.00" style="text-align:right;"></div>
        <div class="total">0,00</div>
        <div class="vat">
            <select name="invoice_line[0][vat]">
                <option value="0.21">21%</option>
                <option value="0.06">6%</option>
                <option value="0.00">0%</option>
            </select>
        </div>
        <input type="hidden" name="invoice_line[0][total]" class="line_total" value="0">
        <input type="hidden" name="invoice_line[0][vat_amount]" class="line_vat_amount" value="0">
        <input type="radio" class="line_selector" style="display:none;">

        <div class="actions"><span class="delLine">Verwijder </span><span>Slepen</span></div>
        <div class="clear"></div>
    </li>

    <li class="extraLine">
        <div class="qty"><input type="text" name="invoice_line[1][qty]" value="1" class="qty_input"></div>
        <div class="description"><textarea name="invoice_line[1][description]"></textarea></div>
        <div class="price"><input type="text" name="invoice_line[1][price]" value="0.00" style="text-align:right;"></div>
        <div class="total">0,00</div>
        <div class="vat">
            <select name="invoice_line[1][vat]">
                <option value="0.21">21%</option>
                <option value="0.06">6%</option>
                <option value="0.00">0%</option>
            </select>
        </div>
        <input type="hidden" name="invoice_line[1][total]" class="line_total" value="0">
        <input type="hidden" name="invoice_line[1][vat_amount]" class="line_vat_amount" value="0">
        <input type="radio" class="line_selector" style="display:none;">

        <div class="actions"><span class="delLine">Verwijder </span><span>Slepen</span></div>
        <div class="clear"></div>
    </li>
    <li class="extraLine">
        <div class="qty"><input type="text" name="invoice_line[2][qty]" value="1" class="qty_input"></div>
        <div class="description"><textarea name="invoice_line[2][description]"></textarea></div>
        <div class="price"><input type="text" name="invoice_line[2][price]" value="0.00" style="text-align:right;"></div>
        <div class="total">0,00</div>
        <div class="vat">
            <select name="invoice_line[2][vat]">
                <option value="0.21">21%</option>
                <option value="0.06">6%</option>
                <option value="0.00">0%</option>
            </select>
        </div>
        <input type="hidden" name="invoice_line[2][total]" class="line_total" value="0">
        <input type="hidden" name="invoice_line[2][vat_amount]" class="line_vat_amount" value="0">
        <input type="radio" class="line_selector" style="display:none;">

        <div class="actions"><span class="delLine">Verwijder </span><span>Slepen</span></div>
        <div class="clear"></div>
    </li>
</ul>

jquery的

$('.vat select').live('keyup', function(e) 
{ 
    if (e.keyCode == 9) 
    {               
        $(this).blur(function(e)
        {
            if($(this).parent().parent().is('li:last'))
            {
                alert('last item');
            }

            e.preventDefault();
            e.stopImmediatePropagation();

        });                 
    } 

});

3 个答案:

答案 0 :(得分:3)

    if($(this).parents("li").is(':last-child'))
    {
        alert('last item');
    }

演示:http://jsfiddle.net/UtHKR/3/

答案 1 :(得分:1)

$('.vat select').live('keyup', function(e) 
{ 
    if (e.keyCode == 9) 
    {               
        $(this).blur(function(e)
        {
            if($(this).parent().parent().is($(':last'))
            {
                alert('last item');
            }

            e.preventDefault();
            e.stopImmediatePropagation();

        });                 
    } 

}); 

答案 2 :(得分:0)

我采取严厉的暗示并给你一些意见。我的解决方案是:

// Use on, so there is only one event listener
$('#sortable').on('keyup', '.vat select', function(event) {
    // Cache your event target
    var $select = $(this);

    if (event.keyCode == 9) {   

        $select.blur(function(event) {
            event.preventDefault();
            event.stopImmediatePropagation();

            var $clostestLi = $select.closest('li');

            if($clostestLi.is(':last-child') == true) {
                alert('last item');
            }
        });                 
}
});

小提琴:http://jsfiddle.net/qLGeb/