2012-08-02 29 views
0

正如你可能會看到下面,我創建了函數getChart(); 它在自己調用時工作,但是當我將它包裝到$(document).ready(function(){...})時不顯示圖表; 我還附上了文件.. 任何幫助,高度重視和感激..感謝名單..谷歌圖形不工作與jQuery按鈕功能

<!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> 
+0

'getChart()'返回一個字符串嗎?做alert(getChart());'告訴我們輸出。 – 2012-08-02 16:52:31

+0

如果我不得不猜測,我猜「這個」並不是指向你想要的地方,但說實話,這裏沒有太多的代碼。 – tsiki 2012-08-02 18:09:17

+0

我附上了上面的代碼..謝謝.. – MegaByteMe 2012-08-02 19:55:35

回答

0

嘗試將其附加到window對象。 喜歡的東西

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

裏調用:

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

注:我沒有測試它。

+0

我已經嘗試過了..但它似乎沒有使用任何jQuery方法.. – MegaByteMe 2012-08-02 20:12:22

+0

讓我感到困惑的是,當我調用getChart();函數在它自己的工作,我可以顯示圖表..但我真的需要的是將該功能附加到事件監聽器..基本上,我得到了101股票表,我希望能夠顯示各個圖表動態,當我點擊一行..該按鈕僅用於測試目的。我會非常歡迎其他建議.. – MegaByteMe 2012-08-02 20:21:10

+0

這很奇怪。當你在不使用jquery函數的情況下對jquery進行測試時,是否包含對jquery的引用? – Vertigo 2012-08-04 14:59:08