Jquery 绑定事件
我有以下代码:
$('#form_field, #button').bind('change click', function() {
// take action
});
它工作正常。但是,我想在“change”用于“#form_field”和“click”用于“#button”时触发相同的操作(而不是当“click”用于“#form_field”时)。
我知道可以使用以下代码来完成:
$('#form_field').bind('change', function() {
// take action
});
$('#button').bind('click', function() {
// take action
});
但是,我不想重复函数内的所有代码(//采取行动)。它看起来无效,每次更改时我都需要编辑两次。
有什么想法吗?
提前致谢
I have the following code:
$('#form_field, #button').bind('change click', function() {
// take action
});
It works fine. However, I want to trigger the same action when 'change' is used for '#form_field' and 'click' for '#button' (not when 'click' is used for '#form_field').
I know that can be done using the following code:
$('#form_field').bind('change', function() {
// take action
});
$('#button').bind('click', function() {
// take action
});
However, I do not want to repeat all the code that is inside the function (// take action). It would look ineffective and I will need to edit it twice every time I make changes to it.
Any ideas?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
??
How about:
??
这应该可以解决问题:
This should do the trick:
我不太确定找到解决这个问题的方法是个好主意。如果它们都做同样的事情,只需为它们创建一个函数来调用所需的事件即可。
I'm not too sure it's that great an idea to find a way around this. If they both do the same thing, just make a function for them to call on the required events.