我可以看到标题中的值。但我对添加有问题。基本上我不知道如何在.ct-chart中添加每个.work-day-graph元素。所以它将像1,25 + 8,25 +0,75 + 0,5 = XY和2,25 + 7,25 + 2,75 + 0.5 = XY。有任何想法吗 ?非常感谢
DEMO:https://jsfiddle.net/1xn1eLfs/6/
JS:
$('.ct-chart').each(function() {
var graphTitle;
$(this).find('.work-day-graph').each(function() {
graphTitle = $(this).attr('title');
var graphTime = graphTitle.substring(graphTitle.lastIndexOf('(') + 1, graphTitle.lastIndexOf('h'));
//graphTimeVal = graphTime.parseInt();
$('<span class=output-val>' + graphTime + '</br>' + '</span>').appendTo('.output');
});
});
答案 0 :(得分:0)
首先,要小心,因为你使用&#34;,&#34;而不是&#34;。&#34;在你的字符串中。要添加这些数字,您必须对它们进行parseFloat。
演示:https://jsfiddle.net/1xn1eLfs/6/
var val = 0;
$('.ct-chart').each(function() {
var graphTitle;
$(this).find('.work-day-graph').each(function() {
graphTitle = $(this).attr('title');
var graphTime = graphTitle.substring(graphTitle.lastIndexOf('(') + 1, graphTitle.lastIndexOf('h'));
graphTime = graphTime.replace(',', '.');
val += parseFloat(graphTime);
$('<span class=output-val>' + graphTime + '</br>' + '</span>').appendTo('.output');
});
});
console.log(val); // 23.5
执行相同操作但是通过ct-chart2或其他任何方式更改第二个ct-chart类,因此您可以单独添加元素。
答案 1 :(得分:0)
这是一个经过修改的JsFiddle: https://jsfiddle.net/3woe2cdq/4/
如果要显示两个图表的总和,则需要在两个图表中单独输出以使其正常工作。
$('.ct-chart').each(function() {
var graphTitle;
// Save the sum of the hours
var graphTimeSum = 0;
$(this).find('.work-day-graph').each(function() {
graphTitle = $(this).attr('title');
var graphTime = graphTitle
.substring(graphTitle
.lastIndexOf('(') + 1, graphTitle
.lastIndexOf('h')
).replace(',', '.');
// Add the current hours to the sum
graphTimeSum += parseFloat(graphTime);
});
// Here we select the output class inside the chart
$('<span class=output-val>' + graphTimeSum + '</br>' + '</span>').appendTo($(this).find('.output'));
});