Javascript从addEventListener返回变量

时间:2013-12-27 14:57:37

标签: javascript jquery json

    var jobj = [];
    var keyChartData = null;
    var chartData = [{
        "name": "John",
        "points": 35654,
        "color": "#7F8DA9",
        "bullet": "0.gif"
    }, {
        "name": "Damon",
        "points": 65456,
        "color": "#FEC514",
        "bullet": "1.gif"
    }, {
        "name": "Patrick",
        "points": 45724,
        "color": "#DB4C3C",
        "bullet": "2.gif"
    }, {
        "name": "Mark",
        "points": 13654,
        "color": "#DAF0FD",
        "bullet": "3.gif"
    } {
        "name": "Mark",
        "points": 43244,
        "color": "#DAF0FD",
        "bullet": "3.gif"
    } {
        "name": "Patrick",
        "points": 64643,
        "color": "#DB4C3C",
        "bullet": "2.gif"
    }, {
        "name": "Patrick",
        "points": 73724,
        "color": "#DB4C3C",
        "bullet": "2.gif"
    }, {
        "name": "John",
        "points": 65672,
        "color": "#DB4C3C",
        "bullet": "2.gif"
    }, ];

    var select = document.getElementById("selector");
    var lookup = {};
    var len = chartData.length;


    for (var i = 0; i < len; i++) {
        var item = chartData[i];
        var name = item.name;
        var option = document.createElement("option");
        option.value = i;
        option.textContent = name;
        select.appendChild(option);
    };


    select.addEventListener('change', function () {
        var selValue = select.options[select.selectedIndex].name;
        for (var i = 0; i < len; i++) {
         var item = chartData[i];

          var name = item.name;
          //alert("selValue: "+selValue+" Key: " +key);

        if(selValue === name){
        jobj.push(JSON.stringify(chartData[i]));
       } 
      } 
       keyChartData = JSON.parse(JSON.stringify(jobj));

    })

alert(keyChartData);

AmCharts.ready(function () {

     // SERIAL CHART    
    chart = new AmCharts.AmSerialChart();
    chart.pathToImages = "http://www.amcharts.com/lib/3/images/";
    chart.dataProvider = keyChartData;
..........
}

这是参考Javascript JSON Display data corresponding to option selected。我曾经问过你。谢谢你的解决方案,我可以解决问题。但是现在,我将keyChartData声明为全局变量,现在我根据所选选项加载JSON数据并将其存储到keyChartData,后者需要在AmCharts.ready函数中捕获以绘制图形。 Outsite函数keyChartdata的值为空,如何去实现这个障碍。请帮助。

1 个答案:

答案 0 :(得分:0)

keyChartdata不是处理程序之外的blacnk。准备就绪时它是空白的。

尝试添加“console.log(1);”更改处理程序和“console.log(2);”准备处理程序。也许你会看到“2”然后是“1”。您必须在退出更改处理程序之前准备就绪,或者只调用另一个将创建AmCharts.AmSerialChart并将keyChartData复制到chart.dataProvider的函数。