首先解除绑定点击然后绑定(jquery)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17250308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:42:34  来源:igfitidea点击:

first unbind click and then bind (jquery)

jqueryonclickbindunbind

提问by Prithviraj Mitra

1.I have a onclick event on,

1.我有一个onclick事件,

$('#locations').click(function(){
$('#train').unbind('click'); 
//do some stuff
}

2.Once the close button is clicked

2.一旦点击关闭按钮

$('.close').click(function(){
//do some stuff
}

3.Then again if I click #train

3.然后如果我点击#train

$('#train').bind('click', function() {
alert('train is clicked');
//do some stuff
}

Now the problem is #train is not firing.Is it to bind the event again on .close function?

现在的问题是#train 没有触发。是否要在 .close 函数上再次绑定事件?

Please suggest.Thanks in advance.

请建议。提前致谢。

回答by krishgopinath

Looking at your question, you do not seem to bind back the clickafter you unbindit, so it will not fire. (Assuming you've kept the sequence of your functionality right). You'll have to do it this way:

看着你的问题,你似乎没有click在你之后绑定unbind它,所以它不会触发。(假设您保持了正确的功能顺序)。你必须这样做:

//Set a function with the action you need to do after you click the train
function  trainClick() {
  alert('train is clicked');
  //do some stuff
}

When you're unbinding, call unbindwith the function name:

解除绑定时,unbind使用函数名称调用:

$('#locations').click(function(){
 $('#train').unbind('click',trainClick);
//do some stuff
}

Then, to bind the click (when #closeis clicked), you'd use :

然后,要绑定点击(#close点击时),您可以使用:

$('.close').click(function(){
  $('#train').bind('click',trainClick);
  //do some stuff
}

NOTE :

笔记 :

A better way would be use onand off, if you are using a version greater than jQuery v1.7 because, well.. then it will not work. In the code above, just replace bindwith onand unbindwith off.

更好的方法是使用onand off,如果您使用的是高于 jQuery v1.7 的版本,因为 .. 那么它将无法工作。在上面的代码,只需更换bindonunbindoff

$('#train').on('click',trainClick);
$('#train').off('click',trainClick);

Hope this helps!

希望这可以帮助!

回答by Sean

BINDING AND UNBINDING HANDLERS

绑定和解绑处理程序

The Key is Scope.

关键是范围。

You must declare and define the function (trainClick(){stuff it does})outside the event-handler so that the other buttons' functions can see it.

您必须声明并定义function (trainClick(){stuff it does})事件处理程序的外部,以便其他按钮的功能可以看到它。

Below is an example.

下面是一个例子。

function trainClick()
{
    alert("train is clicked"); //Notice this function is declared outside event handlers below
}
$('#button1').on("click", trainClick); //this calls the above function

$("#button2").on("click",function(){
    $("#button1").off("click",trainClick); //unbinds button1 from trainClick() function (event handler)
});//End Button 2 click

$("#button3").on("click",function(){
    $("#button1").on("click",trainClick); //binds button1 to trainClick() function (event handler)
});//End Button 2 click