2014-03-03 35 views
0

我在JQuery的这样的绑定事件:)如何检查绑定事件是否被调用?

$j("#chart").bind('jqplotDataClick', 
    function(ev, seriesIndex, pointIndex, data) 
    { 

    }); 

在另一个功能adjustLine(,我要检查,如果上述绑定事件已被或不叫。你知道该怎么做吗?谢谢!

function adjustLine() 
{ 
    // if the jqplotDataClick event is called, do following 

    //else 
} 
+2

添加一个变量到您的eventListener并检查。 – putvande

+2

你最好解释你为什么需要这个?! –

+1

@ A.Wolff是的,它听起来有点像XY问题...... – Alnitak

回答

2

设置一个全局变量作为标志,做的东西,

var flag=false; 
$j("#chart").bind('jqplotDataClick', 
    function(ev, seriesIndex, pointIndex, data) 
    { 
     flag=true; 
    }); 

function adjustLine() 
{ 
    // if the jqplotDataClick event is called, do following 
    if(flag) 
    { 

    } 
} 
+1

不,_don't_使用全局变量,在闭包中使用一个绑定。 – Alnitak

+0

谢谢! Anoop!好主意! –

+0

@Alnitak感谢您的信息。这种方法有什么问题吗? –

0

如果你的目的只是呼吁,一旦处理程序,您可以使用.one()而不是.bind()和一个使用后的处理程序会自动绑定。

1

我会使用data API存储的标志值

$j("#chart").bind('jqplotDataClick', function (ev, seriesIndex, pointIndex, 
data) { 
    j$(this).data('jqplotDataClicked', true); 
}) 


function adjustLine() { 
    if (j$('#chart').data('jqplotDataClicked')) { 
     //clicked 
    } else { 
     //not 
    } 
} 
0

先解除任何以前绑定的事件。这不会让您将同一个事件重新绑定到选择器。

$('#myButton').unbind('click', onButtonClicked) //remove handler 
       .bind('click', onButtonClicked); //add handler 
相关问题