Jak dodać nowy znacznik div, gdy jest zaznaczone pole wyboru przy użyciu jQuery

głosy
0

Jak dodać nowy znacznik div obok sprawdzić skrzynkę, gdy jest zaznaczone pole wyboru, a także wtedy, gdy pole wyboru jest zaznaczone dwa tag dwa div musi być wyświetlacz. Proszę pomóc i uczynić mi rozwiązać ten moduł za pomocą jQuery

Utwórz 30/07/2009 o 06:28
źródło użytkownik
W innych językach...                            


1 odpowiedzi

głosy
2

$(':checkbox').click(function () {
    if ($(this).attr('checked')) {
        // create new div
        var newDiv = $('<div>contents</div>');

        // you can insert element like this:
        newDiv.insertAfter($(this));

        // or like that (choose syntax that you prefer):
        $(this).after(newDiv);
    } else {
        // this will remove div next to current element if it's present
        $(this).next().filter('div').remove();
    }
});

Jeśli wan't aby dodać nowe div obok pola wyboru”Label to najpierw upewnij się, że identyfikacyjnego dla twojego wyboru i że stosowanie atrybutu w etykiet do podłączenia etykiety z wyboru:

<label for="myCb1">test</label>
<input type="checkbox" id="myCb1" value="1" />

Teraz możesz modyfikować kod JS powyżej trochę i gotowe:

$(':checkbox').click(function () {
    // current checkbox id
    var id = $(this).attr('id');

    // checkbox' label
    var label = $('label[for=' + id + ']');

    if ($(this).attr('checked')) {
        // create new div
        var newDiv = $('<div>contents</div>');

        // insert div element
        newDiv.insertAfter(label);
    } else {
        // this will remove div next to current element if it's present
        label.next().filter('div').remove();
    }
});
Odpowiedział 30/07/2009 o 06:34
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more