Javascript 查看最后一个div是否可见,然后禁用“下一步”按钮?

Javascript 查看最后一个div是否可见,然后禁用“下一步”按钮?,javascript,jquery,html,Javascript,Jquery,Html,我有一个跨越多个div的表单,这些div使用jQuery显示和关闭。我想禁用第一个和最后一个div上的next和previous按钮,当它们可见时 这听起来像是一项简单的任务,因为我对jQuery知之甚少,但考虑到我目前的代码,这比我想象的要困难得多 以下是我当前的下一个和上一个按钮功能 var sel = "div[data-type='form']"; var current = $(sel).get(0); $(sel).not(current).hide();

我有一个跨越多个div的表单,这些div使用jQuery显示和关闭。我想禁用第一个和最后一个div上的next和previous按钮,当它们可见时

这听起来像是一项简单的任务,因为我对jQuery知之甚少,但考虑到我目前的代码,这比我想象的要困难得多

以下是我当前的下一个和上一个按钮功能

    var sel = "div[data-type='form']";
    var current = $(sel).get(0);

    $(sel).not(current).hide();

    $("#next").click(function () {
        if ($(form).valid()) {
            current = $(current).next(sel);
            $(current).show();
            $(sel).not(current).hide();
        } 
    });

    $("#prev").click(function () {
        current = $(current).prev(sel);
        $(current).show();
        $(sel).not(current).hide();

    });
下面是目前正在发生的一些事情(注意:出于测试目的,我删除了验证)

请注意,“上一步”按钮可能从一开始就被隐藏。此外,如果需要,可以禁用而不是隐藏。

这可能很有用:

$("#next").click(function () {
    if ($(form).valid()) {
        current = $(current).next(sel);
        $(current).show();
        $(sel).not(current).hide();

        // Last element's index is equal to length - 1
        $(this).attr('disabled', current.index(sel) == $(sel).length - 1);
        // First element's index is equal to 0
        $("#prev").attr('disabled', current.index(sel) == 0);
    }
});

$("#prev").click(function () {
    current = $(current).prev(sel);
    $(current).show();
    $(sel).not(current).hide();

    // Last element's index is equal to length - 1
    $("#next").attr('disabled', current.index(sel) == $(sel).length - 1);
    // First element's index is equal to 0
    $(this).attr('disabled', current.index(sel) == 0);
});

关于

太棒了,这非常有效-但这里到底发生了什么?更具体地说,
if(true)
部分的意思是什么?@user1735913:对不起,
if(true)
部分只是为了避免表单验证,因为我不想每次都麻烦填写表单来查看上一个/下一个按钮是否正常工作。只需将
$(表单).valid()
重新插入即可。哦,好的。理解。非常感谢:)您禁用了它们,但也需要重新启用它们!他说的话。否则,这也是一个有效的答案。谢谢你的帮助:)
$("#next").click(function () {
    if ($(form).valid()) {
        current = $(current).next(sel);
        $(current).show();
        $(sel).not(current).hide();

        // Last element's index is equal to length - 1
        $(this).attr('disabled', current.index(sel) == $(sel).length - 1);
        // First element's index is equal to 0
        $("#prev").attr('disabled', current.index(sel) == 0);
    }
});

$("#prev").click(function () {
    current = $(current).prev(sel);
    $(current).show();
    $(sel).not(current).hide();

    // Last element's index is equal to length - 1
    $("#next").attr('disabled', current.index(sel) == $(sel).length - 1);
    // First element's index is equal to 0
    $(this).attr('disabled', current.index(sel) == 0);
});