Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 行动有时会失败_Javascript - Fatal编程技术网

Javascript 行动有时会失败

Javascript 行动有时会失败,javascript,Javascript,我在一些手术上有个奇怪的问题。我试图像购物车一样计算总物品数量,但有时显示的结果是Nan var thisID = $(this).attr('id'); var itemname = $(this).find('div .name').html(); var itemprice = $(this).find('div .price').html(); if (include(Arrays, thisID)) { var price = $('#each-' + thisID).ch

我在一些手术上有个奇怪的问题。我试图像购物车一样计算总物品数量,但有时显示的结果是Nan

var thisID = $(this).attr('id');

var itemname = $(this).find('div .name').html();
var itemprice = $(this).find('div .price').html();

if (include(Arrays, thisID)) {
    var price = $('#each-' + thisID).children("li").children(".shopp-price").find('em').html();
    var quantity = $('#each-' + thisID).children("li").children(".shopp-quantity").html();
        quantity = parseInt(quantity) + parseInt(1);

        var total = (parseFloat(itemprice) * parseFloat(quantity)).toFixed(2);

        $('#each-' + thisID).children("li").children(".shopp-price").find('em').html(total);
        $('#each-' + thisID).children("li").children(".shopp-quantity").html(quantity);

        var prev_charges = $('.cart-total').html();
        prev_charges = parseFloat(prev_charges) - parseFloat(price);

        prev_charges = (parseFloat(prev_charges) + parseFloat(total)).toFixed(2);
        $('.cart-total').html(prev_charges);
        qte = prev_charges;

} 
问题是,有时它表现得很好,有时则不好。我想知道这是parseFloat()还是parseInt()问题。
这是一个屏幕截图

不要显示完整的代码,尽量只显示操作,否则没有人会阅读。
parseInt(1)
我想你真的在确保这个数字是一个数字。;)html()中的某些内容导致parseInt/parseFloat失败。您认为它与
parseFloat
parseInt
相关的冲动是正确的——您几乎可以肯定,在某个时候,您会将解析不好的值传递给数字。尝试将通过的所有内容都记录到控制台日志中。另外,不要忘记..的
基数
参数。