代码之家  ›  专栏  ›  技术社区  ›  Mohamad

如何使用jQuery确定表行是第一行还是最后一行

  •  1
  • Mohamad  · 技术社区  · 14 年前

    Move Item Up By One Move Item Down By One <tr> 项目已到达表的顶部或底部。

    这是我目前正在使用的,当一个项被上下移动时,它处理交换表行。

        $(".up,.down").click(function(){
            var row = $(this).parents("tr:first");
            if ($(this).is(".up")) {
                row.insertBefore(row.prev());
            } else {
                row.insertAfter(row.next());
            }
        });
    

    我想加一个 嵌套if语句 在第一个if语句下,以及 另一个if语句

    $(row).find('.up').find('input').attr('disabled','true');
    

    表单按钮的类是 up down

    所以 <form class="up"> <input type="submit"> </form> 等。。。

    我不确定这种方法是否是最好的,但我想试试。

    1 回复  |  直到 14 年前
        1
  •  2
  •   mkoryak    14 年前

    您可以尝试在tr上使用prevAll()和nextAll()。如果没有返回结果,则表示所述tr是第一个子或最后一个子。

    $(".up,.down").click(function(){
        var row = $(this).parents("tr:first");
        if ($(this).is(".up")) {
            row.insertBefore(row.prev());
            if(row.prevAll().length == 0){
               //you just moved a row to the top. this is where you disable the button
               row.find('.up').find('input').attr('disabled','true');
               //the row that was the top before had its up disabled, enable it
               row.prev().find('.up').find('input').removeAttr('disabled');
            } 
        } else {
            row.insertAfter(row.next());
            //do same kind of thing here with nextAll
        }
    });