0

我有这个 Javascript 函数,它可以计算多达 30 个不同的字段,因为用户可以添加多达 30 个他们出售的商品并输入数量并显示价格。此代码适用于每个行项目,我最多可以添加 30 个项目并查看它们各自的总数。在表格的底部,我有一个小计区域,这就是我无法动摇这个 $NaN 出现的地方......当查看代码时,它的小计、stantot 和 showtot 变量我得到了 NaN......

$(document).ready(function() {
    var subtotal = 0;
    var stantot = 0;
    var showtot = 0;
    $("input").keyup(function() {
        for (i = 0; i <= 30; i++) {
            var unitp = $("#unitp" + i).val();
            var casep = $("#casep" + i).val();
            var units = $("#units" + i).val();
            var cases = $("#cases" + i).val();
            var st_disc = $("#st_disc").val();
            var sh_disc = $("#sh_disc").val();

            var unitr = (unitp * units);
            var caser = (casep * cases);
            var result = (unitr + caser);
            var st_disc_fix = (st_disc / 100);
            var sh_disc_fix = (sh_disc / 100);
            var st_disc_solo = (st_disc_fix * result);
            var sh_disc_solo = (sh_disc_fix * result);
            var disc_total = (st_disc_fix + sh_disc_fix);
            var disc_whole = (disc_total * result);

            var disc = (result - disc_whole);
            var st_disc_tot = (result - disc_whole);
            var sh_disc_tot = (result - disc_whole);

            $("#line" + i).val('$' + result.toFixed(2));
            $("#disc" + i).val('$' + disc.toFixed(2));
            subtotal += parseInt(result);
            stantot += parseInt(st_disc_tot);
            showtot += parseInt(sh_disc_tot);
        }
        $("#totretail").val('$' + subtotal.toFixed(2));
        $("#standiscount").val('$' + stantot.toFixed(2));
        $("#showdiscount").val('$' + showtot.toFixed(2));

        var totship = ($("#totship").val() * 1);
        var tottax = ($("#tottax").val() * 1);

        var finaltotal = (subtotal + stantot + showtot + totship + tottax);
        $("#total").val('$' + finaltotal.toFixed(2));

    });
});
4

5 回答 5

1

Here's what I would do.

First, I would create a function that takes a string and guarantees that a number is returned:

function toNumber(val) {
    var num = parseFloat(val);
    num = isNaN(num) ? 0 : num;
    return num;
}

In this case, any NaN values are converted to zero. You can alter this to suit the needs of your application.

Then, I would update your variable declarations where you're pulling in the field values like so:

var unitp = toNumber($("#unitp" + i).val());
var casep = toNumber($("#casep" + i).val());
var units = toNumber($("#units" + i).val());
var cases = toNumber($("#cases" + i).val());
var st_disc = toNumber($("#st_disc").val());
var sh_disc = toNumber($("#sh_disc").val());

Since all proceeding calculations are based on these variables, if you guarantee that none of these are NaN, then none of the following values will be NaN either (barring exceptional circumstances).

于 2012-09-05T13:49:15.837 回答
1

圣格比尔的回答效果很好,这是我的工作代码,供将来参考...

$(document).ready(function() {
var subtotal = 0;
var stantot = 0;
var showtot = 0;
$("input").keyup(function() {
    for (var i = 0; i <= 30; i++) {
        var unitp = parseFloat($("#unitp" + i).val()) || 0;
        var casep = parseFloat($("#casep" + i).val()) || 0;
        var units = parseFloat($("#units" + i).val()) || 0;
        var cases = parseFloat($("#cases" + i).val()) || 0;
        var st_disc = parseFloat($("#st_disc").val()) || 0;
        var sh_disc = parseFloat($("#sh_disc").val()) || 0;

        var unitr = (unitp * units);
        var caser = (casep * cases);
        var result = (unitr + caser);
        var st_disc_fix = (st_disc / 100);
        var sh_disc_fix = (sh_disc / 100);
        var st_disc_solo = (st_disc_fix * result);
        var sh_disc_solo = (sh_disc_fix * result);
        var disc_total = (st_disc_fix + sh_disc_fix);
        var disc_whole = (disc_total * result);

        var disc = (result - disc_whole);
        var st_disc_tot = (result - disc_whole);
        var sh_disc_tot = (result - disc_whole);

        $("#line" + i).val(result.toFixed(2));
        $("#disc" + i).val(disc.toFixed(2));
        subtotal += parseFloat((unitp * units) + (casep * cases));
        stantot += parseFloat(st_disc_tot);
        showtot += parseFloat(sh_disc_tot);
    }
    $("#totretail").val(subtotal.toFixed(2));
    $("#standiscount").val(stantot.toFixed(2));
    $("#showdiscount").val(showtot.toFixed(2));

    var totship = ($("#totship").val() * 1);
    var tottax = ($("#tottax").val() * 1);

    var finaltotal = (subtotal + stantot + showtot + totship + tottax);
    $("#total").val(finaltotal.toFixed(2));

    });
}); 
于 2012-09-06T14:32:23.897 回答
0

改变

for (i = 0; i <= 30; i++) {

for (var i = 0; i <= 30; i++) {

于 2012-09-05T13:39:00.187 回答
0

您应该检查空字符串和空字符串,以便像这样更改所有语句

var unitp = $("#unitp" + i).val();

var unitp = $("#unitp" + i).val()===''?0:$("#unitp" + i).val();

或者

var unitp = $("#unitp" + i).val()||0;
于 2012-09-05T13:44:55.413 回答
0

将所有用户输入传递给 parseFloat 或 parseInt(取决于您是否需要小数点)

$(document).ready(function() {
    var subtotal = 0;
    var stantot = 0;
    var showtot = 0;
    $("input").keyup(function() {
        for (var i = 0; i <= 30; i++) {
            var unitp = parseFloat($("#unitp" + i).val()) || 0;
            var casep = parseFloat($("#casep" + i).val()) || 0;
            var units = parseInt($("#units" + i).val(),10) || 0;
            var cases = parseInt($("#cases" + i).val(),10) || 0;
            var st_disc = parseFloat($("#st_disc").val()) || 0;
            var sh_disc = parseFloat($("#sh_disc").val()) || 0;

            var unitr = (unitp * units);
            var caser = (casep * cases);
            var result = (unitr + caser);
            var st_disc_fix = (st_disc / 100);
            var sh_disc_fix = (sh_disc / 100);
            var st_disc_solo = (st_disc_fix * result);
            var sh_disc_solo = (sh_disc_fix * result);
            var disc_total = (st_disc_fix + sh_disc_fix);
            var disc_whole = (disc_total * result);

            var disc = (result - disc_whole);
            var st_disc_tot = (result - disc_whole);
            var sh_disc_tot = (result - disc_whole);

            $("#line" + i).val('$' + result.toFixed(2));
            $("#disc" + i).val('$' + disc.toFixed(2));
            subtotal += parseInt(result);
            stantot += parseInt(st_disc_tot);
            showtot += parseInt(sh_disc_tot);
        }
        $("#totretail").val('$' + subtotal.toFixed(2));
        $("#standiscount").val('$' + stantot.toFixed(2));
        $("#showdiscount").val('$' + showtot.toFixed(2));

        var totship = ($("#totship").val() * 1);
        var tottax = ($("#tottax").val() * 1);

        var finaltotal = (subtotal + stantot + showtot + totship + tottax);
        $("#total").val('$' + finaltotal.toFixed(2));

    });
});

这将默认NaN条目为 0,并且应该按照您的预期进行。

不确定您期望整数或浮点数的值,因此根据需要进行交换。

编辑:添加了几个 parseInt,所以你知道如何调用它们。

于 2012-09-05T13:45:19.153 回答