解除jQuery偶数处理程序的绑定 [英] Unbind jQuery even handler

查看:62
本文介绍了解除jQuery偶数处理程序的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在几分钟前回答这个问题,并为此示例做了准备我自己:

I try to answer this question a few minutes ago and prepared this example for myself :

<script>
  function trialMethod()
  {
    alert('On Submit Run!'); return true;
  }
  function trialMethod2()
  {
    alert('On Submit Run trialMethod2!'); return true;
  }
</script>

<form id="aspnetForm" onsubmit="trialMethod();">
    <input type="submit">
</form>

为什么第一次取消绑定不起作用:

Why the first unbind doesn't work :

<input type="button" id="btnTrial1" value="UNBIND 1" 
   onclick="$('#aspnetForm').unbind('submit', trialMethod);">

但这适用于trialMethod2方法:

But this one works for the trialMethod2 method :

<input type="button" id="btnTrial2" value="UNBIND 2" 
   onclick="$('#aspnetForm').bind('submit', trialMethod2).unbind('submit', trialMethod2);">

推荐答案

由于jQuery的事件模型,第一个取消绑定的情况不起作用. jQuery将每个事件处理程序函数存储在可以通过$("#foo").data('events')访问的数组中. unbind函数仅在此数组中查找给定的函数.因此,您只能使用bind()

The first unbind scenario doesn't work, because of jQuery's event model. jQuery stores every event handler function in an array that you can access via $("#foo").data('events'). The unbind function looks just for given function in this array. So, you can only unbind() event handlers that were added with bind()

这篇关于解除jQuery偶数处理程序的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆