0

我在表格中选中的复选框发生了一些非常奇怪的事情。我在表格中构建了一个发票计算器,你可以在这里看到它,我的目的是一旦我自动检查输入(Iva 或 Irpef),它们就不会在总金额中计算。正如您在我链接的示例中看到的那样,如果您选中Irpef Input 复选框,它运行良好,Irpef 不在总金额中计算。问题是我的输入复选框 IVA 不会发生同样的情况,为什么?代码完全相同。

当我试图让它们单独工作时,很好,两者都工作得很好,所以看起来他们不能一起工作,可能是?发生了什么?在这个问题上我需要你的帮助。

谢谢

这里是与问题相关的代码:

function tally(selector) {
    var total = 0;
    $('p.editable_number').each(function() {
        total += parseInt($(this).text()) || 0;
        $('#subtotal').html(total);
        if($("#iva").is(':checked')){
            $('#subtotal').html((total).toFixed(2));
            $('#total').html((total*0.00).toFixed(2));
            $('#total1').html((total*0.15).toFixed(2));
            $('#total2').html((total*0.85).toFixed(2));
        }
        else {
            $('#subtotal').html((total).toFixed(2));
            $('#total').html((total*0.21).toFixed(2));
            $('#total1').html((total*0.15).toFixed(2));
            $('#total2').html((total*1.06).toFixed(2));
        }
         if($("#irpef").is(':checked')){
            $('#subtotal').html((total).toFixed(2));
            $('#total').html((total*0.21).toFixed(2));
            $('#total1').html((total*0.00).toFixed(2));
            $('#total2').html((total*1.21).toFixed(2));
        }
        else {
            $('#subtotal').html((total).toFixed(2))
            $('#total').html((total*0.21).toFixed(2));
            $('#total1').html((total*0.15).toFixed(2));
            $('#total2').html((total*1.06).toFixed(2));
        }
        })
        }

$('#irpef').change(function() {
             tally('p#subtotal');
             tally('p#total');
             }); 
$('#iva').change(function() {
             tally('p#subtotal');
             tally('p#total');
             }); 
4

1 回答 1

1

试试这个,现在应该可以了。我认为你试图做一个 else if 并做几次而不是一次(每个函数)。刷新 我添加了变量来跟踪总计/小计和增值税/irpf。 each()遍历所有 p.editable_number 元素,因此您多次执行相同的代码,每次覆盖以前的结果时,您都会得到正确的结果,因为它是结束循环。现在您只需获取一次值,然后计算相关的小计、增值税、irpf 和总值(基于是否检查增值税或 irpf 的条件)。

于 2012-10-24T07:55:08.657 回答