如何检查是否调用了绑定事件?

时间:2014-03-03 16:13:06

标签: javascript jquery

我在JQuery中有一个绑定事件,如下所示:

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

    });

在另一个函数adjustLine()中,我想检查是否已经调用了上面的绑定事件。你知道怎么做吗?谢谢!

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

    //else
}

4 个答案:

答案 0 :(得分: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 :(得分: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
    }
}

答案 2 :(得分:0)

如果您的意图是只调用一次处理程序,则可以使用.one()而不是.bind(),处理程序将在一次使用后自动解除绑定。

答案 3 :(得分:0)

首先取消绑定任何先前绑定的事件。这不会让您将同一事件重新绑定到选择器。

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