如何使用jQuery对发票申请税,折扣

时间:2010-08-17 20:54:19

标签: javascript jquery

如何申请税,折扣小计并计算总额?

我的这些段落带有以下ID

<p id="subtotal">15000</p>
<p id="tax">10</p> // In percentage
<p id="discount">1000</p>
<p id="grandtotal"></p> // Grandtotal will be calculated and displayed here using jquery

总计将是15000 +(1500 //税) - (1000 //折扣)= 15500

我如何使用jQuery计算?

3 个答案:

答案 0 :(得分:2)

var subtotal = parseFloat( $('#subtotal').text() );
...

$('#grandtotal').text( grandTotal );

其余的是vanilla javascript,除了设置或获取。

答案 1 :(得分:2)

var subtotal = parseFloat( $('#subtotal').text());
var taxRate = parseFloat( $('#tax').text());
var disc = parseFloat( $('#discount').text());

var taxAmount = subtotal * (taxRate/parseFloat("100")); //15000 * .1

var yourGrandTotal = subtotal + (taxAmount) - (disc);

//update div with the val
$('#grandtotal').text(yourGrandTotal);

答案 2 :(得分:0)

首先,完全滥用段落元素<p>。相反,请使用以下内容:

HTML

<div id="subtotal-and-taxes">
    <var id="subtotal">15000</var>
    <var id="tax">10</var> // In percentage
    <var id="discount">1000</var>
    <var id="grandtotal"></var> // Grandtotal will be calculated and displayed here using jquery
    <!-- or use span elements -->
</div>

CSS

#subtotal-and-taxes var {
    display:block
    margin-top:5px;
    margin-bottom:5px;
}

接下来,您可以使用jQuery获取对这些元素的引用,并使用html()函数读取其内容:

var subtotal = $("#subtotal-and-taxes #subtotal").html();
var tax = $("#subtotal-and-taxes #tax").html();
var discount = $("#subtotal-and-taxes #discount").html();

然后使用非jQuery JavaScript计算grandtotal的值。不幸的是,你的等式有点令人困惑,所以我不会为你拼出来。