带参数的 jQuery 绑定和取消绑定事件
我正在尝试将事件绑定到包含参数的文本框。下面的 keep 看起来好像应该这样做,但每次页面加载时,它都会被执行。
jQuery(function(){
jQuery('#textbox').bind('click', EventWithParam('param'));
});
每次加载页面时都会使用该参数调用该事件。这可能不起作用,因为不支持带参数的事件。如果是的话,还有其他路线吗?
I am trying to bind an event to a textbox
that contains parameters. The following keep looks as if it should do it, but every time the page loads, it gets executed.
jQuery(function(){
jQuery('#textbox').bind('click', EventWithParam('param'));
});
The event gets called with that parameter every time the page loads. This may not work because events with parameters are not supported. If so, is there another route?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将事件参数作为第二个参数传递给bind()。它不是直接回调参数,但也许您想使用它:
来自 jQuery 文档:
绑定
You can pass event parameters as second arguments to bind(). It is not direct callback parametes but maybe you would like to use it:
From jQuery documentation:
bind
要绑定接受参数的函数,请使用匿名函数作为参数的闭包。
您的示例正在执行
EventWithParam
函数,然后尝试绑定到该函数调用的结果。调用
unbind
而不指定函数将取消绑定指定类型的所有处理程序事件(包括匿名函数)。如果您想专门取消绑定该函数,则需要为其提供一个名称,如下所示:To bind a function that takes parameters, use an anonymous function to act as a closure over the parameters.
Your example is executing the
EventWithParam
function, and then trying to bind to the result of that function call.Calling
unbind
without specifying a function will unbind all handlers for the specified type of event (including the anonymous function). If you want to unbind that function specifically, you'll need to provide it with a name, like this:bdukes 是完全正确的。我添加我的响应是因为我最近发现了一些与 jQuery 的绑定/取消绑定函数和匿名函数及其事件名称空间有关的有趣内容。如上所示,绑定事件的调用方式如下。
jQuery('#textbox').bind('click',function(){EventWithParam('param')});
要取消绑定,即使用户也必须调用 ...
jQuery('#textbox').unbind('click');
实际上,删除所有 onClick 事件。
现在出现在 jQuery 命名空间中。如果您有一个匿名函数,您希望附加到单击事件并稍后取消绑定,请按如下操作。
jQuery('#textbox').bind('click.yourNameSpace',function(){EventWithParam('param')});
然后要解除绑定,只需调用解除绑定函数,如下所示。
jQuery('#textbox').unbind('click.yourNameSpace');
其余的 onClick 事件将保留。
bdukes is exactly right. I'm adding my response as I've recently found something interesting having to do with jQuery's bind/unbind function and anonymous functions, its event name spaces. Previously shown above the bind event is called as follows.
jQuery('#textbox').bind('click',function(){EventWithParam('param')});
To unbind this even the user must call ...
jQuery('#textbox').unbind('click');
In effect, removing all onClick events.
Now comes in jQuery name spaces. If you have an anonymous function you wish to attach to the click event and later unbind do it as follows.
jQuery('#textbox').bind('click.yourNameSpace',function(){EventWithParam('param')});
Then to unbind simple call the unbind function as follows.
jQuery('#textbox').unbind('click.yourNameSpace');
The rest of your onClick events will remain.
原因是 EventWithParam 在绑定中作为函数被调用。换句话说,您将点击事件绑定到调用
EventWithParam('param')
的结果。这应该可行:现在您已将 Even 绑定到一个函数,当事件触发时,该函数将调用
EventWithParam('param')
The reason is that EventWithParam is being called as a function right there in your binding. Phrased another way, you're binding the click event to the result of calling
EventWithParam('param')
. This ought to work:Now you've bound the even to a function that, when the event fires, will call
EventWithParam('param')