在jQuery中绑定多个事件 [英] Binding multiple events in jQuery
问题描述
我有一个自定义jQuery插件,它将更改
事件绑定到表单元素,在这种情况下一些输入
元素。 更改
事件用于向某些其他组件指示发生了更改。
I have a custom jQuery plugin which binds a change
event to a form element, in this case some input
elements. The change
event is used to indicate to some other components that a change has taken place.
这样可以正常运行应该。
This works as it should.
但是,我遇到了将额外的更改
事件绑定到同一个<$>的需要c $ c> input 元素 - 以便其他组件可以对事件起作用。我不想更换插件的更改
,之后只需运行一个附加功能。
However, I've come across the need to bind an additional change
event to the same input
elements -- so that additional components can act on the event. I don't wish to replace the plugin's change
, simply run an additional function afterwards.
有没有办法在jQuery中绑定多个事件,或者,实现此目的的最佳方法是什么?
Is there any way to bind multiple events in jQuery, or, what is the best method for accomplishing this?
推荐答案
您可以绑定多个处理程序同样的事件。像往常一样绑定第二个更改:
You can bind multiple handlers for the same event. Bind the second change as you normally would:
<input type="text" id="a" />
$("#a").change(function() { alert("1"); });
$("#a").change(function() { alert("2"); });
$("#a").change(); // alerts 1, then 2
这篇关于在jQuery中绑定多个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!