计算所有行的税收

时间:2014-11-21 18:36:17

标签: php jquery html

<input type="text"   value="<?php echo $order_quan; ?>" class="quantity" size='3' "/>
</td>



<input type="text"  value="<?php echo $Price; ?>" class="price" size='10' />
</td>

我需要帮助弄清楚如何将所有行的总税收合并起来。

我在获得小计,税金和总计的每一行都没有问题。

  <td width=60px><label class="subtotal" ><?php echo $Total_price; ?></label></td> 

  <td width=60px><label class="tax" ><?php echo $tax; ?></label></td>


  <td width=60px><label class="total_of" ><?php echo $total_of; ?></label></td> 

所有行的无税小计将在这里。我工作得很好

<td align= 'left'>Sub Total:  <label class="grandtotal"></label></td>
 </tr>

所有行的总税收将在这里......不工作

<td align= 'left'>Total Tax:  <label class="tax_total"></label></td>
</tr>  

JavaScript:我尝试了很多东西,但无法弄明白。非常感谢任何帮助

 <script type="text/javascript">

    $(function() {
     CalculateTotal();

    $('.quantity , .price').on('change', function() {


    UpdateTotals(this);
});
});

 function UpdateTotals(elem) {
// This will give the tr of the Element Which was changed
var $container = $(elem).parent().parent();


var quantity = $container.find('.quantity').val();
var price = $container.find('.price').val(); 

var subtotal = parseInt(quantity) * parseFloat(price);
var tax = parseInt(quantity) * parseFloat(price) * .0625;
var total_of = parseInt(quantity) * parseFloat(price) * 1.0625;


$container.find('.subtotal').text(subtotal.toFixed(2));
$container.find('.tax').text(tax.toFixed(2));
 $container.find('.total_of').text(total_of.toFixed(2));


CalculateTotal();
}

function CalculateSubTotals() {
// Calculate the Subtotals when page loads for the 
// first time
var lineTotals = $('.subtotal');
var quantity = $('.quantity');
var price= $('.price');
$.each(lineTotals, function(i){
    var tot = parseInt($(quantity[i]).val()) * parseFloat($(price[i]).val());
    $(lineTotals[i]).text(tot.toFixed(2));

   });
}

function CalculateTotal() {
// This will Itearate thru the subtotals and
// claculate the grandTotal and Quantity here
var lineTotals = $('.subtotal');
var quantityTotal = $('.quantity');


var grandTotal = 0.0;
var totalQuantity = 0;



$.each(lineTotals, function(i) {
    grandTotal += parseFloat($(lineTotals[i]).text());
    totalQuantity += parseInt($(quantityTotal[i]).val())


});

$('.totalquantity').text(totalQuantity);
$('.grandtotal').text(parseFloat(grandTotal).toFixed(2));

}  

</script>

2 个答案:

答案 0 :(得分:0)

数学将是

(item1 +(item1 * .0625))+(item2 +(item2 * .0625))+ ....... + n = TotalWithTax

最简单的方法是将所有itemtotals加起来,而不是税,所以

item1 + item2 + item3 ...... + n = TotalWithoutTax

TotalWithTax - TotalWithoutTax = TaxTotal。

在你的情况下,TotalWithoutTax将是所有小计的总和。

答案 1 :(得分:0)

我想通了,这似乎有助于找到总税。但由于某种原因,税收总额不会长期固定在小数点后两位。

   $('.grandtotal').text(parseFloat(grandTotal).toFixed(2));
   $('.tax_total').text(.0625*parseFloat(grandTotal).toFixed(2));

感谢帮助。