将变量相乘并在框中显示结果

时间:2015-10-20 02:22:19

标签: javascript jquery

我在尝试将totalPallets乘以每托盘价格(25美元)然后在productSubTotal框中显示时出现问题。使用现在的代码,quatity total显示,但是当我试图获得价格结果时,它不会显示操作。此外,如果我尝试从代码中更改anythung,整个事情就会崩溃。如果有人能帮助我,我会感激不尽。感谢

// UTILITY FUNCTIONS 

function IsNumeric(n) {
    return !isNaN(n);
}

function calcTotalPallets() {
    var totalPallets = 0;
    $(".num-pallets-input").each(function() {
        var thisValue = parseInt($(this).val());
        if ( (IsNumeric(thisValue)) &&  (thisValue != '') ) {
            totalPallets += parseInt(thisValue);
        };
    });
    $("#quantitytotal").val(totalPallets);
}

function calcProdSubTotal() {
    var prodSubTotal = 0;
    $(".totalprice").each(function() {
        var valString = parseInt(totalPallets) * multiplier;
        prodSubTotal += parseInt(valString);
    });
    $("#product-subtotal").val(CommaFormatted(prodSubTotal));
};


// "The Math" is performed pretty much whenever anything happens in the quanity inputs
$('.num-pallets-input').bind("focus blur change keyup", function(){
    // Caching the selector for efficiency 
    var $el = $(this);

    // Grab the new quantity the user entered
    var numPallets = CleanNumber($el.val());
    var totalPallets = CleanNumber($el.val());
    var prodSubTotal = CleanNumber($el.val());

    // Find the pricing
    var multiplier = $el
        .parent().parent()
        .find("td.price-per-pallet span")
        .text();
    };
    // Calcuate the overal totals
    calcProdSubTotal();
    calcTotalPallets();    
});
function CommaFormatted(amount) {
    var delimiter = ","; 
    var i = parseInt(amount);
    if(isNaN(i)) { return ''; }
    i = Math.abs(i);
    var minus = '';
    if (i < 0) { minus = '-'; }
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    amount = "$" + minus + n;
    return amount;
}

});

0 个答案:

没有答案