[英]jQuery - How to remove the listeners to all from elements in single function?
In my form, I have no.of form elements, I would like to remove all the listeners from the form elements.. how to achieve this? 在我的表单中,我有no.of表单元素,我想从表单元素中删除所有的监听器..如何实现这一点?
here is my try, but not working..is this only way to remove each one by one? 这是我的尝试,但没有工作..这是唯一的方法来逐一删除?
var testFunction = function () {
$form = $('form'),
$name = $form.find('#name'),
$code = $form.find('#code'), //it will have more events..
$city = $form.find('#city'), //it will have more events..
$native = $form.find('#native'), //it will have more events..
$gender = $form.find('#gender'), //it will have more events..
$select = $form.find('#select'); //it will have more events..
//sample.
$name.on('focus keydown input', function(e){
console.log(e.target.value);
});
$select.on('change', function(e){
console.log($(this).val());
});
}
testFunction();
$('#unbindForm').on('click', function(){
$('form').addClass('highlight').off(); //i can't remove all events?
});
what would be the correct approach. 什么是正确的方法。 in the
off
do i need to send all listeners what i used? 在
off
我需要发送所有听众我使用的? ( change keydown input
)? (
change keydown input
)?
Use .unbind() : With no arguments, .unbind()
removes all handlers attached to the elements. 使用.unbind() :没有参数,
.unbind()
删除附加到元素的所有处理程序。
$('form *').unbind();
OR 要么
$('form *').off();
For jQuery 1.7 > .off()
is not available. 对于jQuery 1.7 >
.off()
不可用。 Above 1.7, both the methods work. 1.7以上,这两种方法都有效。 - Reference
- 参考
If you want to remove all the events from the elements just use .off()
without passing any anrguments into it. 如果要从元素中删除所有事件,只需使用
.off()
而不将任何anrguments传递给它。
$('form *:not(#name)').off();
And here I have excluded the element with id
name since you said that it does not have any events at all. 在这里,我已经排除了具有
id
名称的元素,因为你说它根本没有任何事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.