从属性JavaScript计算总和

从属性JavaScript计算总和,javascript,sum,Javascript,Sum,如何计算放入li表中的许多不同get属性的总和?我想在每次添加/删除li表时更新总数。。。代码如下: Javascript: var ele = info[plus.indexOf(this)]; var ul = document.getElementById("cart_id"); var li = document.createElement("LI"); var title = ele.getAttribute("data-title"); var price = parseIn

如何计算放入li表中的许多不同get属性的总和?我想在每次添加/删除li表时更新总数。。。代码如下: Javascript:

var ele = info[plus.indexOf(this)];

var ul = document.getElementById("cart_id");

var li = document.createElement("LI");

var title = ele.getAttribute("data-title");

var price = parseInt(ele.getAttribute("data-price"));

li.appendChild(document.createTextNode(title+" "+price));   
ul.appendChild(li);//So far so good!!


/*Update! when I try this code below, I get current value from (data-price) X 3? 
So when I press 299 I get value 897? Why does it count like this? 
I want to sum the total!? I guess I have to put [i] inside there 
but I don't know which way, I doesn't work if I try:  
total += parseInt(listItem[i].ele.getAttribute("data-price")); */

  var total = 0;

  listItem = ele.getAttribute("data-price");

  for (var i=0; i < listItem.length; i++) 
  { 
  total += parseInt(ele.getAttribute("data-price")); 
  }

    document.querySelector("#totalPrice").value = total;
var ele=info[plus.indexOf(this)];
var ul=document.getElementById(“购物车id”);
var li=document.createElement(“li”);
var title=ele.getAttribute(“数据标题”);
var price=parseInt(ele.getAttribute(“数据价格”));
li.appendChild(document.createTextNode(title+“”+price));
ul.儿童(li)//到现在为止,一直都还不错!!
/*更新!当我尝试下面的代码时,我从(数据价格)x3获得当前值?
所以当我按299时,我得到值897?为什么这样算数?
我想把总数加起来!?我想我得把它放进去
但我不知道该怎么做,如果我尝试:
total+=parseInt(listItem[i].ele.getAttribute(“数据价格”)*/
var合计=0;
listItem=ele.getAttribute(“数据价格”);
对于(变量i=0;i
HTML:


运货马车

总价:
  • iPad欧元;299)/li>

因此,我想从“产品UL类”中获取价格(本例中为data price=“299”)并将其放入“cart UL类”。

在您的代码中
price
int
(来自
var price=parseInt(ele.getAttribute(“data price”);
)并将其分配给
var arr=price中的数组
arr


您可能打算用元素数组填充
arr

您可以将新价格添加到现有总价中,也可以重新计算总价。例如:

function getSum() {
   var totalPrice = 0;
   $('li').each(function () {
      totalPrice += parseInt($(this).data('price'));
   });
   return totalPrice;
}
没有jquery:

var list= document.getElementById("ul");
var listItem = list.getElementsByTagName("li");

var totalPrice = 0;

for (var i=0; i < listItem.length; i++) { 
    totalPrice += parseInt(listItem[i].getAttribute('data-price')); 
}
var list=document.getElementById(“ul”);
var listItem=list.getElementsByTagName(“li”);
var totalPrice=0;
对于(var i=0;i
这是一个完全有效的打捞器

函数recalculareTotal(){
var总计=0,
lis=document.getElementsByTagName(“li”);
对于(变量i=0;i
它没有jQuery,可以完成所有需要的操作—每次添加和删除项目并计算总数

更新

  • 代码中的“li”标记未关闭

  • iPad欧元;299)/li>

  • 我不知道“ele”变量在代码中的确切含义是什么,但首先应该得到整个列表(UL),然后对其元素(li)进行交互,并获得该列表中每个li的“数据价格”属性

  • 更新的plunker,请参见函数
    recalcularetatal2


    我该怎么做?或者如何显示getAttribute(“数据价格”)中的总计;?我必须使用数组吗?谢谢谢谢,没有Jquery有什么想法吗?只添加了JavaScript?@toppen,没有jquery!谢谢,但我得到这个错误:TypeError:list是null@toppen您需要根据html更新第一行。发布您的html,我们可以提供帮助。始终在
    parseInt
    上设置基础。如果值为
    09
    ,则代码将无法工作。它将
    09
    解释为八进制中的9,这是无效的。然后返回
    NaN
    。现在,
    totalPrice
    将是
    NaN
    。结果:现在是你所期望的。将基数设置为
    10
    ,如果返回
    NaN
    ,则在末尾添加
    |124; 0
    。然后调用
    totalPrice+=parseInt(listItem[i].getAttribute('data-price'),10)|0。你也需要发布你的html,非常感谢,但我还是无法让它工作。我得到NaN作为输出!你的代码中有Nan还是我的plunker示例?你用什么浏览器?我的代码中有NaN。我用Mozilla。。。lis=document.getElementsByTagName(“li”);我不确定这里用什么作为标记名。如果你看我的HTML代码??谢谢我不能尝试你的代码,因为它不完整(你能创建一个plunker吗?),但我注意到你的代码的第一件事:
    var price=parseInt(ele.getAttribute(“数据价格”)var arr=价格之后“arr”也是一个整数。然后你正在为(var i=0;iHi)做一个循环
    ,我仍然无法让它工作,你能看看我更新的代码吗?谢谢!
    
    var list= document.getElementById("ul");
    var listItem = list.getElementsByTagName("li");
    
    var totalPrice = 0;
    
    for (var i=0; i < listItem.length; i++) { 
        totalPrice += parseInt(listItem[i].getAttribute('data-price')); 
    }
    
    function recalculareTotal(){
    
      var total = 0,
          lis = document.getElementsByTagName("li");
    
      for (var i = 0; i < lis.length; i++ ){
        total += parseInt(lis[i].getAttribute("data-price"));
      }
    
      document.querySelector("#totalPrice").value = total;
    }