【问题标题】:google graph does not work with jquery button function谷歌图表不适用于 jquery 按钮功能
【发布时间】:2012-08-02 16:44:21
【问题描述】:

如下所示,我创建了函数 getChart(); 它在自己调用时有效,但在我将其包装到 $(document).ready(function(){...}); 时不显示图表; 我还附上了文件。。 任何帮助都受到高度重视和赞赏..thanx..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Google Visualization API Sample</title>
  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">

    function getChart(){
    google.load('visualization', '1', {packages: ['corechart']});
    function drawVisualization() {
       // Populate the data table.

        var dataTable = google.visualization.arrayToDataTable([
           ['Mon', 20, 28, 38, 45],
           ['Fri', 68, 66, 22, 15]
        // Treat first row as data as well.
        ], true);   

        // Draw the chart.
        var chart = new     google.visualization.CandlestickChart(document.getElementById('visualization'));
        chart.draw(dataTable, {legend:'none', width:600, height:400});
    }

    google.setOnLoadCallback(drawVisualization);

    }

    $(document).ready(function(){
        $('#idbutton').click(function(){
            getChart();
        });
    });


  </script>
</head>
<body>
<input id="idbutton" type="button" value="button" />
<div id="visualization"></div>
</body>
</html>

【问题讨论】:

  • getChart() 是否返回字符串?执行alert(getChart()); 并告诉我们输出结果。
  • 如果我不得不猜测,我猜“this”并没有指向你想要它指向的地方,但老实说,这里没有太多代码可供参考。
  • 我附上了上面的代码..谢谢..
  • 当我运行这段代码并调试它(shift+ctrl+j)时,我看到body标签消失了..

标签: javascript jquery ajax button google-visualization


【解决方案1】:

尝试将它附加到窗口对象。 类似的东西

$(document).ready(function(){
    window.getChart = function() { //Code of getChart here };
});

然后调用它:

$(document).ready(function(){
    $('#idbutton').click(function(){
        window.getChart();
    });
});

注意:我没有测试它。

【讨论】:

  • 令我困惑的是,当我调用 getChart();函数本身就可以工作,我可以显示图表。但我真正需要的是将该函数附加到事件侦听器。基本上,我有一个包含 101 只股票的表格,我希望能够显示当我点击一行时,相应的图表会动态变化。该按钮仅用于测试目的。我非常欢迎其他建议..
  • 这很奇怪。在不使用 jquery 函数的情况下进行测试时是否包含了对 jquery 的引用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多