jQuery checkbox and select - sum the values and add in var

前端 未结 6 1116
礼貌的吻别
礼貌的吻别 2020-12-17 07:10

The explanation is below the code:

The code for HTML is :

Add-ons

6条回答
  •  死守一世寂寞
    2020-12-17 07:39

    I have done complete bins for above query. here is demo link as well.

    Demo: http://codebins.com/bin/4ldqp85

    HTML

    Add-ons

    Add-on Number 1 - 10 QR
    Add-on Number 2 - 20 QR
    Add-on Number 3 - 40 QR
    Add-on Number 4 - 60 QR
    I want more add-ons User total usage:

    JQuery

    $(function() {
        $('input[name="ch1"]:checkbox').change(function() {
            var total = 0;
            $('input[name="ch1"]:checked').each(function() {
                total += parseInt($(this).val());
            });
            total += parseInt($('#more').val());
            $('#usertotal').html(total);
        });
        $('#more').change(function() {
            var selectVal = $(this).val().trim();
            if (parseInt(selectVal) > 0) {
                $("#span").html("more addons: " + selectVal);
            } else {
                $("#span").html("");
            }
            $('input[name="ch1"]:checkbox').trigger('change');
        });
    });
    

    Demo: http://codebins.com/bin/4ldqp85

提交回复
热议问题