2014-04-19 235 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 
});