如何将数组值打印到变量?

时间:2015-01-28 10:43:23

标签: javascript jquery html css ajax

在这里,我使用了Chartist js作为面积图。每种货币都有不同的价值。我已将每个货币值存储在每个变量中。在ajax调用之后,我添加了值,如var arrVal = $('#changeCurrency a')。attr('id','');.但它没有指向当前。当我给出类似var arrVal = dataCur11意味着它会好起来的。这个问题的解决方案是什么?

HTML

<div id="changeCurrency">
    <a href="javascript:void(0)" id="dataCur11" class="active">UAE</a>
    <a href="javascript:void(0)" id="dataCur21">USD</a>
    <a href="javascript:void(0)" id="dataCur31">SGD</a>
    <a href="javascript:void(0)" id="dataCur41">INR</a>
    <a href="javascript:void(0)" id="dataCur51">POUND</a>
</div>

<div id="graphContainer"></div>

JS

$(document).on('click', '#changeCurrency a', function(){

        $.ajax({
            url: 'partials/graph.html',
            method: 'GET',
            success: function (data, supportsForeignObject) {
                $('#graphContainer').html('');
                $('#graphContainer').append(data);
                $('#chart-loader').fadeOut();   
                var dataCur11 = [50, 53, 54, 52, 56, 55, 53, 50],
                    dataCur21 = [20, 53, 34, 42, 26, 55, 53, 50],
                    dataCur31 = [40, 53, 24, 32, 56, 35, 53, 56],
                    dataCur41 = [50, 33, 54, 30, 22, 20, 30, 52],
                    dataCur51 = [20, 33, 54, 30, 22, 20, 30, 52];
                var arrVal = $('#changeCurrency a').attr('id','');
                // Support
                var supportsForeignObject = document.implementation.hasFeature("www.http://w3.org/TR/SVG11/feature#Extensibility","1.1");
                if(supportsForeignObject) {
                    var labelsVal = [22+" <span>Dec'14</span>", 24+" <span>Dec'14</span>", 26+" <span>Dec'14</span>", 28+" <span>Dec'14</span>", 30+" <span>Dec'14</span>", 1+" <span>Jan'15</span>", 3+" <span>Jan'15</span>", 5+" <span>Jan'15</span>"]
                } else {
                    var labelsVal = [22+" Dec'14", 24+" Dec'14", 26+" Dec'14", 28+" Dec'14", 30+" Dec'14", 1+" Jan'15", 3+" Jan'15", 5+" Jan'15"];
                }


                setTimeout(function(){
                    $('.val-pos').addClass('current-pos');
                    new Chartist.Line('.ct-chart', {
                            labels: labelsVal,
                            series:[arrVal]
                        }, {
                        axisY: {
                            offset: 35,
                            scaleMinSpace: 60
                        },
                        low: 0,
                        showArea: true
                    });
                }, 1000);
            }
        });
    });

1 个答案:

答案 0 :(得分:0)

请检查以下代码,希望这会对您有所帮助

$(document).on('click', '#changeCurrency a', function () {

    var dataCur11 = [50, 53, 54, 52, 56, 55, 53, 50],
        dataCur21 = [20, 53, 34, 42, 26, 55, 53, 50],
        dataCur31 = [40, 53, 24, 32, 56, 35, 53, 56],
        dataCur41 = [50, 33, 54, 30, 22, 20, 30, 52],
        dataCur51 = [20, 33, 54, 30, 22, 20, 30, 52];
    var arrVal = eval($(this).attr('id'));
                console.log(arrVal);
    });

小提琴:http://jsfiddle.net/cdq95qLt/

所以你的代码就像这样

$(document).on('click', '#changeCurrency a', function(){

        $.ajax({
            url: 'partials/graph.html',
            method: 'GET',
            success: function (data, supportsForeignObject) {
                $('#graphContainer').html('');
                $('#graphContainer').append(data);
                $('#chart-loader').fadeOut();   
                var dataCur11 = [50, 53, 54, 52, 56, 55, 53, 50],
                    dataCur21 = [20, 53, 34, 42, 26, 55, 53, 50],
                    dataCur31 = [40, 53, 24, 32, 56, 35, 53, 56],
                    dataCur41 = [50, 33, 54, 30, 22, 20, 30, 52],
                    dataCur51 = [20, 33, 54, 30, 22, 20, 30, 52];
                var arrVal = eval($(this).attr('id'));
                // Support
                var supportsForeignObject = document.implementation.hasFeature("www.http://w3.org/TR/SVG11/feature#Extensibility","1.1");
                if(supportsForeignObject) {
                    var labelsVal = [22+" <span>Dec'14</span>", 24+" <span>Dec'14</span>", 26+" <span>Dec'14</span>", 28+" <span>Dec'14</span>", 30+" <span>Dec'14</span>", 1+" <span>Jan'15</span>", 3+" <span>Jan'15</span>", 5+" <span>Jan'15</span>"]
                } else {
                    var labelsVal = [22+" Dec'14", 24+" Dec'14", 26+" Dec'14", 28+" Dec'14", 30+" Dec'14", 1+" Jan'15", 3+" Jan'15", 5+" Jan'15"];
                }


                setTimeout(function(){
                    $('.val-pos').addClass('current-pos');
                    new Chartist.Line('.ct-chart', {
                            labels: labelsVal,
                            series:arrVal
                        }, {
                        axisY: {
                            offset: 35,
                            scaleMinSpace: 60
                        },
                        low: 0,
                        showArea: true
                    });
                }, 1000);
            }
        });
    });