JQuery嵌套if / else if语句

时间:2014-12-17 08:53:23

标签: jquery if-statement nested

我在页面中有三个输入字段,它们以不同的组合显示(.append),还有第四个div显示该组合的总和:它可能是: a (总是on), a + b a + c a + b + c 。我使用 if / else if 语句,但idoesn适用于 a + b + c 。有人可以帮忙吗?谢谢。 代码我使用:

    $( document ).ready(function() {
    $(".page").on("change keyup keydown paste propertychange bind mouseover", function(){

    var iva4 = $("#total_amount").val();
    var iva22 = $("#total22_amount").val();
    var iva0 = $("#total0_amount").val();

    if (($("#total_amount").length) && ($("#total22_amount").length)) {

        var totaleIva = (parseFloat(iva4) + parseFloat(iva22));

    } else if (($("#total_amount").length) && ($("#total0_amount").length)) {

        var totaleIva = (parseFloat(iva4) + parseFloat(iva0));

    } else if ((("total_amount").length) && ($("#total0_amount").length) && ($("#total22_amount").length) ) {

        var totaleIva = (parseFloat(iva4) + parseFloat(iva22) + parseFloat(iva0));
    }

    $("#totale-somma").val(parseFloat(totaleIva).toFixed(2));


    if( !$.trim( $('.subtotale').html() ).length ) {
        $('#somma-finale').css("display", "none");
    } else {
        $('#somma-finale').css("display", "block");
    }


   });

   });

2 个答案:

答案 0 :(得分:1)

问题是if

的顺序
 $(document).ready(function () {
     $(".page").on("change keyup keydown paste propertychange bind mouseover", function () {

         var iva4 = $("#total_amount").val();
         var iva22 = $("#total22_amount").val();
         var iva0 = $("#total0_amount").val();

         if ((("#total_amount").length) && ($("#total0_amount").length) && ($("#total22_amount").length)) {

             var totaleIva = (parseFloat(iva4) + parseFloat(iva22) + parseFloat(iva0));
         } else if (($("#total_amount").length) && ($("#total22_amount").length)) {

             var totaleIva = (parseFloat(iva4) + parseFloat(iva22));

         } else if (($("#total_amount").length) && ($("#total0_amount").length)) {

             var totaleIva = (parseFloat(iva4) + parseFloat(iva0));

         }

         $("#totale-somma").val(parseFloat(totaleIva).toFixed(2));


         if (!$.trim($('.subtotale').html()).length) {
             $('#somma-finale').css("display", "none");
         } else {
             $('#somma-finale').css("display", "block");
         }


     });
 });

答案 1 :(得分:1)

假设所有总字段都可以在html DOM中使用,下面是计算总计的解决方案 -

$( document ).ready(function() {
   $(".page").on("change keyup keydown paste propertychange bind mouseover", function(){
        // below code will convert values to float otherwise to 0
        var iva4 = parseFloat($("#total_amount").val()) || 0;
        var iva22 = parseFloat($("#total22_amount").val()) || 0;
        var iva0 = parseFloat($("#total0_amount").val()) || 0;

        var totaleIva = iva4 + iva22 + iva0;

        $("#totale-somma").val(parseFloat(totaleIva).toFixed(2));


        if( !$.trim($('.subtotale').html()).length ) {
            $('#somma-finale').hide();
        } else {
            $('#somma-finale').show();
        }

    });

});