减去两个值

时间:2013-09-16 11:14:27

标签: javascript subtraction

需要使用simpleCart_total减去&_SESSION['usr']['points']才能获得最终总数。 基本上javascript值从数据库值中减去。

任何想法如何做到这一点?

<table><tbody>
<tr>
<td class='odd'><?php echo htmlentities($_SESSION['usr']['usr'], ENT_QUOTES, 'UTF-8');    ?></td>
<td class='even'>&nbsp;</td>
</tr>
<tr>
<td class='odd'>Your Value Points: </td>
<td class='even'><?php echo htmlentities($_SESSION['usr']['points'], ENT_QUOTES, 'UTF-8'); ?></td>
</tr>
<tr>
<td class='odd'>&nbsp;</td>
<td class='even'>&nbsp;</td>
</tr>

<tr><td class='odd'>Total Items: </td><td class='even'><span class='simpleCart_quantity'></span></td></tr>
<tr><td class='odd'>SubTotal: </td><td class='even'><span class='simpleCart_total'></span></td></tr>
<tr>
<td class='odd'>&nbsp;</td>
<td class='even'>&nbsp;</td>
</tr>
<tr>
<td class='odd'>Total</td>
<td class='even'>&nbsp;</td>
</tr>

</tbody>
</table>

1 个答案:

答案 0 :(得分:0)

我只是将一个id添加到放置用户点的行中,将id添加到simpleCart_total并将id添加到总结果中                                                           您的价值点:                                                      

    <tr><td class='odd'>Total Items: </td><td class='even'><span id='simpleCart_quantity' class='simpleCart_quantity'></span></td></tr>
    <tr><td class='odd'>SubTotal: </td><td class='even'><span id='simpleCart_total' class='simpleCart_total'>4</span></td></tr>
    <tr>
    <td class='odd'>&nbsp;</td>
    <td class='even'>&nbsp;</td>
    </tr>
    <tr>
    <td class='odd'>Total</td>
    <td class='even' id="total">&nbsp;</td>
    </tr>

    </tbody>
    </table>

然后每次添加或删除项目时都调用此函数:

<script>
function total(){
    var points =  parseFloat(document.getElementById('points').innerHTML);
    var simpleCart_total = parseFloat(document.getElementById('simpleCart_total').innerHTML);

    var total = document.getElementById('total');

    total.innerHTML = points-simpleCart_total;

}
</script>

修改

我不知道它是否会起作用,但你可以试试。 备份你的文件javascript / cart.js,然后找到并替换这段代码:

for(var c=0;a.childNodes[c];c++){var d=a.childNodes[c];if(d.className&&d.className.match(/item_[^ ]+/)){var e=/item_[^ ]+/.exec(d.className)[0].split("_");if(e[1]=="add"||e[1]=="Add"){var f=[];f.push(d);var g=simpleCart.Shelf.addToCart(b.id);simpleCart.addEventToArray(f,g,"click");d.id=b.id}else{b[e[1]]=d}}

为此:

for(var c=0;a.childNodes[c];c++){var d=a.childNodes[c];if(d.className&&d.className.match(/item_[^ ]+/)){var e=/item_[^ ]+/.exec(d.className)[0].split("_");if(e[1]=="add"||e[1]=="Add"){var f=[];f.push(d);var g=simpleCart.Shelf.addToCart(b.id);simpleCart.addEventToArray(f,g,"click");d.id=b.id}else{b[e[1]]=d}}var points=parseFloat(document.getElementById('points').innerHTML);var simpleCart_total = parseFloat(document.getElementById('simpleCart_total').innerHTML);var total=document.getElementById('total');total.innerHTML=points-simpleCart_total;

不要忘记将id“total”添加到要显示结果的单元格