开发者

javascript sum returning NaN error

i have a javascript shopping basket, in which the sum is returning a NaN error,almost every time. in the code i have

    $('#add-to-basket select').selectbox();
    $('#contents select').selectbox().change(function (e) {
        var product = $(this).parents('.product');
        var ppu     = product.find('.ppu').val();
        product.find('.price .wrapper .value').text($(this).val() * ppu);

        var total   = 0;

        $('.product .price .value').each(function (index, value) {
            total += new Number($(value));
        });

        var form = $(this).parents('form');
        form.ajaxSubmit(function () {
        });

        $('#total .value').text(total);
    });

i tried using parsefloatm but still开发者_JS百科 it dosn't work...


$(value) gives you the jQuery-wrapped element, not the actual value.

You want $(value).val() instead if the elements are form inputs, or $(value).text() if not.

Also, instead of new Number(...) you should just use Number(...), or even +...:

$('.product .price .value').each(function (index, value) {
    total += +$(value).val();
});

See this question for the difference between new Number and Number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜