2014-04-19 67 views
1

我無法捕捉到我的rails 3.1應用程序中最基本的jqplot點擊事件。我想捕捉任何點擊我的圖表。我創建了一個簡單的測試頁面,仍然不能正常工作:當我點擊圖上的jqplot點擊事件沒有觸發

<head> 
    <%= javascript_include_tag "jquery"%> 
    <%= javascript_include_tag "jquery.jqplot.min.js"%> 
    <%= stylesheet_link_tag "jquery.jqplot.min.css" %> 
</head> 
<body> 
<p>Chart test</p> 
<div id='chart1'></div> 
</body> 
<script> 
$(document).ready(function(){ 
    var plot1 = $.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]); 

    function myClickHandler(ev, gridpos, datapos, neighbor, plot) { 
    alert('you have triggered click action'); 
    } 
    $.jqplot.eventListenerHooks.push(['jqplotClick', myClickHandler]); 

}); 
</script> 

什麼也沒有發生。我嘗試了許多不同的方法。

我正在使用版本3.1.0的jquery-rails gem(版本1.11的jquery)和版本1.0.8r1250的jqplot。

我現在甚至不知道還有什麼要解決的問題。任何幫助將非常感激。

回答

1

的解決方案是建立在打印之前成立eventListenerHooks:

<script> 
$(document).ready(function(){ 


    function myClickHandler(ev, gridpos, datapos, neighbor, plot) { 
    alert('you have triggered click action'); 
    } 
    $.jqplot.eventListenerHooks.push(['jqplotClick', myClickHandler]); 

    var plot1 = $.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]); //moved 


}); 
</script> 
1

爲什麼不使用內置的jQuery的事件處理程序?它不需要創建事件監聽器鉤子...

$.jqplot('chart1',...); 
$('#chart1').bind('jqplotClick', function(ev, gridpos, datapos, neighbor, plot) { 
    //handle jqplotClick 
});