如何在堆栈中添加值

时间:2015-12-17 10:36:01

标签: javascript html

我有一堆数字,用于在图表中显示... 我想添加这些数字并在标签中显示数字...... 我怎么能这样做?



 $(function ZoneClick() {
    $("[id*=rbtnZone]").click(function() {
        var row = $(this).closest('tr');
        var branchId = $(row).find('[id*=hfBranchId]').val();
        var rbtlSales = $("#<%= rbtlSales.ClientID%>");
        var selectedValue = rbtlSales.find("input:checked").val();
        
        $.ajax({
            url: '<%=ResolveUrl("~/Corporate/Sales.aspx/GetZoneData") %>',
            data: "{'rbtlSales':'" + selectedValue + "'}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                var labels = [];
                var datas = [];
                $.each(data.d, function(i, item) {
                    var l = item.split('-')[0];
                    var d = item.split('-')[1];
                    var dd = d | 0;
                    labels.push(l);
                    datas.push(dd);
                });

                var barChartLocData =
        {
            labels: labels,
            datasets:
            [
                {
                    fillColor: "indianred",
                    highlightFill: "red",
                    data: datas
                }
            ]
        };
                var ctx = document.getElementById("canvas").getContext("2d");
                new Chart(ctx).HorizontalBar(barChartLocData, {
                    responsive: true,
                    scaleFontColor: "#000",
                    showTooltips: false,
                    onAnimationComplete: function() {
                        var ctx = this.chart.ctx;
                        ctx.font = this.scale.font;
                        ctx.fillStyle = this.scale.textColor
                        ctx.textAlign = "right";
                        ctx.textBaseline = "center";
                        this.datasets.forEach(function(dataset) {
                            dataset.bars.forEach(function(bar) {
                                ctx.fillText(bar.value, bar.x + 15, bar.y);
                            });
                        });
                    }
                });
            },
            error: function(response) {
            },
            failure: function(response) {
            }
        });
    });
});
&#13;
&#13;
&#13;

最后一行dd包含值...我想添加它们并将其显示在标签中......

此致 Sajin A

2 个答案:

答案 0 :(得分:2)

使用array.push()array.pop()array用作stack

http://www.w3schools.com/jsref/jsref_push.asp

http://www.w3schools.com/jsref/jsref_pop.asp

一些例子:

var initial_arr = [ 1, 4 ];
console.log( initial_arr );  // output :  [ 1, 4 ]

initial_arr.push(6);
console.log( initial_arr );  // output :  [ 1, 4 , 6]

initial_arr.push(9);
initial_arr.push(7);
console.log( initial_arr );  // output :  [ 1, 4 , 6, 9, 7]

var popedValue = initial_arr.pop();
console.log( popedValue );  // output :  7
console.log( initial_arr );  // output :  [ 1, 4 , 6, 9]

答案 1 :(得分:0)

解决方案很简单...... var lblmsg =“总销售额”是+ + eval(datas.join('+')); document.getElementById('&lt;%= lblTotal.ClientID%&gt;')。innerHTML = lblmsg;