2014-03-02 69 views
0

看看:http://jsfiddle.net/MLsAu/14/ 在Asp.Net page_load,程序計算總和價格和總價和總折扣。但我想用jquery計算當用戶更改下拉列表值(數量產品)。我知道一些代碼是錯誤的,但我不知道哪裏錯了。例如,當用戶將第一項數量從2更改爲1時,SumPrice是錯誤的!jquery總和項目的價格和總價格

我測試了這段代碼,但它不好。

   var sumPrice = 0; 
       $('tr').each(function() { 
        sumPrice += parseInt(quantity * parseInt($(this).closest('tr').find('.ffour').text())); 
       }); 
       $('#txtSumPrice').html(sumPrice); 

回答

1

您必須在每次更改事件中將總和,折扣和價格設置爲0,否則將採用最後一個可用值。請找到更正的小提琴here

$('.drop').on('change',function() { 
    $('#txtSumDiscount').text('0'); 
    $('#txtSumPrice').text('0'); 
    $('#txtTotal').text('0'); 
    var tr = $(this).closest('tr'); 
    var price = $(tr).find('.ffour').html() - $(tr).find('.fthree').html(); 
    $(tr).find('.fone').html(price * $(this).val()); 

    var sum = 0, discount = 0, total =0; 

    $(this).closest('#dvCart').find('tr td.fone').each(function(){ 
     var parent = $(this).parent(); 
     var q = parseFloat($(parent).find('select.drop').val()); 
     sum += parseFloat($(parent).find('td.ffour').text()) * q; 
     discount += parseFloat($(parent).find('td.fthree').text()) * q; 
    }); 

    $('#txtSumPrice').html(sum); 
    $('#txtSumDiscount').html(discount); 
    $('#txtTotal').html(sum - discount); 
}); 
+0

txtSumPrice是所有項目價格之和,但在您的代碼中,txtSumPrice顯示一個項目價格。 –

+0

在asp.net code-behind中,計算價格總額和頁面加載折扣。但是當用戶更改一個項目的數量值時,我想更改txtSumPrice。 –

+0

@ user3234297更新了小提琴。檢查http://jsfiddle.net/Fv5RS/2/ – rakhi4110