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

时间:2022-05-26 22:39:47

Here, i have used chartist js for area chart. Each currency having the different value. I have stored each currency value in each variable. After ajax call i added value like that var arrVal = $('#changeCurrency a').attr('id',' ');. But its not pointing currectly. When i given like that var arrVal = dataCur11 means it coming fine. What is the solution for this problem?

在这里,我使用了图表的js。每一种货币具有不同的价值。我在每个变量中存储了每个货币值。在ajax调用之后,我添加了这样的值,比如var arrVal = $('#changeCurrency a')。attr(' id ',' ');。但它的指向并不简单。当我这样做时,var arrVal = dataCur11意味着它会好起来。这个问题的解决方案是什么?

HTML

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

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 个解决方案

#1


0  

please check the below code ,hope this will help you

请检查下面的代码,希望这对你有帮助。

$(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);
    });

fiddle :http://jsfiddle.net/cdq95qLt/

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

so your code will be like this

你的代码是这样的

$(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);
            }
        });
    });

#1


0  

please check the below code ,hope this will help you

请检查下面的代码,希望这对你有帮助。

$(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);
    });

fiddle :http://jsfiddle.net/cdq95qLt/

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

so your code will be like this

你的代码是这样的

$(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);
            }
        });
    });