0

I'm attempting to add 2 values together with jQuery. I have a table with these values:

<table>
<tr id="fc_cart_foot_subtotal">
<td class="fc_col2">$7.95</td>
</tr>
<tr id="fc_cart_foot_shipping">
<td class="fc_col2">$4.00</td>
</tr>
<tr id="fc_cart_foot_total">
<td class="fc_col2">$7.95</td>
</tr>
</table>

I need to add the value of #fc_cart_foot_subtotal .fc_col2:

<tr id="fc_cart_foot_subtotal">
<td class="fc_col2">$7.95</td>
</tr>

to the value of #fc_cart_foot_shipping .fc_col2:

<tr id="fc_cart_foot_shipping">
<td class="fc_col2">$4.00</td>
</tr>

and have the value of #fc_cart_foot_total .fc_col2 updated

<tr id="fc_cart_foot_total">
<td class="fc_col2">$7.95</td>
</tr>

So in this example, the first subtotal value of $7.95 should add $4.00 to give a total of $11.95. The subtotal and shipping cost will change, so I'll need to be able to "grab" those values as they change and use them in an equation.

4

1 に答える 1

0

ドル文字列を加算用の数値に変換するには:

function parseDollar(str) {
   return +str.substr(1);
}

次に、数値を加算して正しくフォーマットします。

$('#fc_cart_foot_total .fc_col2').text('$' + (
    parseDollar($('#fc_cart_foot_subtotal .fc_col2').text()) +
    parseDollar($('#fc_cart_foot_shipping .fc_col2').text())
).toFixed(2));

「-$1.00」などの負のドル値を取得できる可能性がある場合は、次のように変更parseDollarします。

function parseDollar(str) {
    return +str.replace(/\$/, '');
}
于 2011-01-18T07:01:08.743 に答える