简体   繁体   English

计算购物车发票的总价

[英]computing total price of shopping cart invoice

In my code after users choose products and go to cart page, my cart page creates an html structure like this for all total prices of selected products: below here x indicates a number which is id of good that I fetch from Session, so every time these differ, also the number of fields changes every time based on user's choice 在用户选择产品并转到购物车页面后的代码中,我的购物车页面会为所选产品的所有总价格创建一个html结构,如下所示:x表示我从Session中获取的商品编号,因此每次这些不同,字段的数量每次根据用户的选择而变化

<span class="total-x"> some value</span>
<span class="total-x"> some value</span>
<span class="total-x"> some value</span>
.
.
.
.
<span class="total-x"> some value</span>

for example if cart gets id=3 form session after some other codes, page creates a field like this: 例如,如果购物车在其他一些代码后获得id = 3表单会话,则页面将创建一个类似如下的字段:

 <span class="total-3"> some value [has been computed]</span>

I want to sum up value of those field and put in a field like this: 我想总结这些字段的值,并放入如下字段:

<span id=total-all> </span> 

I ask for every which would lead me to over come this. 我要求所有能使我克服这个问题的方法。

Try to use start with selector to identify that unknown class, 尝试使用start with selector来识别该未知类,

$("#total-all").text($("span[class^='total-']").get().reduce(function(a,b){
  return a + (+$(b).text());
},0));

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM