2

現在の合計を保持することになっている変数があります。このループの各パスで、現在の合計に amount を追加する必要があります。未定義または NaN のいずれかになるため、何かが欠けているに違いありません。

$('#btnSubmit').click(function(e) {
    var totalSqft;
    $('.fieldset').each(function() {
        var sqft;
        var width = $(this).find('input:eq(0)').val();
        var height = $(this).find('input:eq(1)').val();
        var type = $(this).find('select').val();
        if (type == 'tri') {
            sqft = (width * height) / 2;
        } else {
            sqft = (width * height);
        };
        totalSqft += sqft;
        alert('this ' + type + ' is ' + width + ' wide and ' + height + ' high, for a total of ' + sqft + ' square feet');
    });
    alert('Done.  Total Sq Ft is ' + totalSqft);
})​
4

1 に答える 1

7

値を 0 に初期化する必要があります。

var totalSqft = 0;

それ以外の場合は に初期化されundefinedundefined+ 数値はNaNです。

于 2012-04-18T00:26:55.797 に答える