2

jQueryを使用してオンとオフで表示されている複数のdivにまたがるフォームがあります。最初と最後の div の次と前のボタンが表示されている場合、これらのボタンを無効にしたいと思います。

これは、私が 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();

    });

そして、ここに現時点で何が起こっているかのフィドルがありますhttp://jsfiddle.net/GZ9H8/6/

4

2 に答える 2

1

これは機能します(注: テスト目的で検証を削除しました)。

$("#next").click(function () {
    if (true) {
        current = $(current).next(sel);
        $(current).show();
        $(sel).not(current).hide();
        if (!$(current).next(sel).get(0)){
            $(this).hide();
        }
        if ($(current).prev(sel).get(0)){
             $("#prev").show();
        }
    }
});

$("#prev").click(function () {
    current = $(current).prev(sel);
    $(current).show();
    $(sel).not(current).hide();
    if ($(current).next(sel).get(0)){
       $("#next").show();
    }
    if (!$(current).prev(sel).get(0)){
        $(this).hide();
    }
});

前のボタンはおそらく最初から非表示にする必要があることに注意してください。また、必要に応じて非表示にする代わりに無効にすることもできます。

于 2012-11-05T16:38:13.717 に答える
1

これは役に立つかもしれません:

$("#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);
});

よろしく

于 2012-11-05T16:40:47.410 に答える