Jquery购物车计算

时间:2014-05-06 07:48:43

标签: javascript jquery

有小提琴example。我在html中找到了价格并尝试了总数但是在点之后不允许这样做

Jquery代码:

$(document).ready(function() {

    var Arrays=new Array();

    $('#wrap li').mousemove(function(){

        var position = $(this).position();

        $('#cart').stop().animate({

                left   : position.left+'px',

            },250,function(){

        });         
    }).mouseout(function(){

    }); 

    $('#wrap li').click(function(){

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

        var itemname  = $(this).find('div .name').html();
        var itemprice = $(this).find('div .price').html();
        var prID = $(this).attr('rel');
        var target="&virtuemart_product_id[]="+prID+"&quantity[]=1";
        $('#checkout').closest('a').attr('href', function(i, currentAttribute){
        return currentAttribute + target;
    });             
        if(include(Arrays,thisID))
        {
            var price    = $('#each-'+thisID).children(".shopp-price").find('em').html();
            var quantity = $('#each-'+thisID).children(".shopp-quantity").html();
            quantity = parseInt(quantity)+parseInt(1);

            var total = parseInt(itemprice)*parseInt(quantity);

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

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

            prev_charges = parseInt(prev_charges)+parseInt(total);
            $('.cart-total span').html(prev_charges);

            $('#total-hidden-charges').val(prev_charges);
        }
        else
        {
            Arrays.push(thisID);

            var prev_charges = $('.cart-total span').html();
            prev_charges = parseInt(prev_charges)+parseInt(itemprice);

            $('.cart-total span').html(prev_charges);
            $('#total-hidden-charges').val(prev_charges);

            $('#left_bar .cart-info').append('<div class="shopp" id="each-'+thisID+'"><div class="label">'+itemname+'</div><div class="shopp-price"> $<em>'+itemprice+'</em></div><span class="shopp-quantity">1</span><img src="http://blog.urbanomic.com/sphaleotas/archives/Ecology_without_Nature/2011-08-31_-_OOOIII__More_Detailed_Schedule/2011-08-31_-_OOOIII__More_Detailed_Schedule_files/icon_delete13.gif" class="remove" /><br class="all" /></div>');

            $('#cart').css({'-webkit-transform' : 'rotate(20deg)','-moz-transform' : 'rotate(20deg)' });
        }

        setTimeout('angle()',200);
    }); 


    $('.remove').livequery('click', function() {

        var deduct = $(this).parent().children(".shopp-price").find('em').html();
        var prev_charges = $('.cart-total span').html();

        var thisID = $(this).parent().attr('id').replace('each-','');

        var pos = getpos(Arrays,thisID);
        Arrays.splice(pos,1,"0")

        prev_charges = parseInt(prev_charges)-parseInt(deduct);
        $('.cart-total span').html(prev_charges);
        $('#total-hidden-charges').val(prev_charges);
        $(this).parent().remove();

        var prID = $("#" + thisID ).attr('rel');

        var target="&virtuemart_product_id[]="+ prID + "&quantity[]=1";
        $('#checkout').closest('a').attr('href', function(i, currentAttribute){
        return currentAttribute.replace(target, "");
    }); 

    }); 

    $('#Submit').livequery('click', function() {

        var totalCharge = $('#total-hidden-charges').val();

        $('#left_bar').html('Total Charges: $'+totalCharge);

        return false;

    }); 

});

function include(arr, obj) {
  for(var i=0; i<arr.length; i++) {
    if (arr[i] == obj) return true;
  }
}
function getpos(arr, obj) {
  for(var i=0; i<arr.length; i++) {
    if (arr[i] == obj) return i;
  }
}

示例:

产品1价格2.90

产品2价格1.090

只允许2 + 1 = 3

(我的购物车jquery代码从第255行开始)

我该如何解决?

此致

1 个答案:

答案 0 :(得分:0)

您需要使用parseFloat()而不是parseInt(),因为parseInt会将其转换为整数,因此会向下舍入十进制数

Updated fiddle