添加从字符串中提取的值
Addition of extract value from the string
如您所见,我从标题中提取了价值。但是我对添加有疑问。基本上我不知道如何在 .ct-chart 中添加每个 .work-day-graph 元素。所以它会像 1,25+8,25+0,75+0,5 = XY 和 2,25+7,25+2,75+0,5 =XY。有任何想法吗 ?非常感谢
演示: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');
});
});
首先要小心,因为您使用的是“,”而不是“.”。在你的字符串中。要添加这些数字,您必须对它们进行 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-chart class 更改为 ct-chart2 或其他,这样您就可以独立添加元素。
这是修改后的 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'));
});
如您所见,我从标题中提取了价值。但是我对添加有疑问。基本上我不知道如何在 .ct-chart 中添加每个 .work-day-graph 元素。所以它会像 1,25+8,25+0,75+0,5 = XY 和 2,25+7,25+2,75+0,5 =XY。有任何想法吗 ?非常感谢
演示: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');
});
});
首先要小心,因为您使用的是“,”而不是“.”。在你的字符串中。要添加这些数字,您必须对它们进行 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-chart class 更改为 ct-chart2 或其他,这样您就可以独立添加元素。
这是修改后的 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'));
});