Migration of working .each (... change ()) to .each (.live ("change ...")) does not work jQuery

I have a list of inputs that load in the finished document, while others load dynamically. I need to summarize them all, because I need to check that they all do not sum more than 99.

The problem is that with .live () I cannot get it to work, but it works with .change ().

/* this works */
$(".percentage").each(function() {
    $(this).change(function() {
        var sum = 0;
        $(".percentage").each(function() {
            sum += $(this).val();
        });
        alert(sum);
    });
});

/* this don't */
$(".percentage").each(function() {
    $(this).live("change keyup focus click keydown", function() {
        var sum = 0;
        $(".percentage").each(function() {
            sum += $(this).val();
        });
        alert(sum);
    });
});

Thank you in advance!

+3
source share
1 answer

, , live. live() DOM, . , , , DOM "", "", , .

Try:

$('.percentage').live('change keyup focus click keydown', function() {
    var sum = 0;

    $(".percentage").each(function() {
        sum += $(this).val();
    });

    alert(sum);
});

, val() , :

$(".percentage").each(function() {
    sum += parseInt($(this).val(), 10);
});
+3

Source: https://habr.com/ru/post/1795225/


All Articles