admin管理员组文章数量:1294327
Could someone tell me how you can deactivate the event onchange and reactivate it
$(':checkbox').each(function (k, v) {
$(this).onchange = null;//
$(this).off() // works but I do not know how to reactivate onchange event
})
Edit 1: Let me explain you my problem ... I'm using bootstrap toggle onmy checkbox. But maybe this is only a detail.
SO my problem is .. I have a "reset" button on my form. When I click on it, I have a function who loops on each checkbox of my form trying to "reset" each checkbox. By reseting my checkbox I mean setting them to their initial value. . . but doing so I trigger an "onchange" function present later in my code. And I do not want to trigger that "onchange" function
Could someone tell me how you can deactivate the event onchange and reactivate it
$(':checkbox').each(function (k, v) {
$(this).onchange = null;//
$(this).off() // works but I do not know how to reactivate onchange event
})
Edit 1: Let me explain you my problem ... I'm using bootstrap toggle onmy checkbox. But maybe this is only a detail.
SO my problem is .. I have a "reset" button on my form. When I click on it, I have a function who loops on each checkbox of my form trying to "reset" each checkbox. By reseting my checkbox I mean setting them to their initial value. . . but doing so I trigger an "onchange" function present later in my code. And I do not want to trigger that "onchange" function
Share edited Aug 11, 2016 at 15:29 zm455 asked Aug 11, 2016 at 15:15 zm455zm455 4991 gold badge11 silver badges26 bronze badges 3- .(this).prop('disabled',true).delay (500).prop('disabled',false) – Reuben Gomes Commented Aug 11, 2016 at 15:17
-
So you want to disable the
onchange
event and than reenable it? Why don't you just exit the function and not unbind the event. – epascarello Commented Aug 11, 2016 at 15:18 -
What do you mean by Temporarily exactly ? And you do not need the
.each
here. – DavidDomain Commented Aug 11, 2016 at 15:24
4 Answers
Reset to default 7I would remend one of two options.
First, you can unbind and rebind the handlers by giving the function a name:
function handleCheckboxClick(e) {
//...
}
$(':checkbox').off('change');
$(':checkbox').on('change', handleCheckboxClick);
The other option is to create a variable that you set to true
or false
as needed to specify whether to execute the handler:
$(':checkbox').on('change', function(e) {
if (!window.shouldExecuteCheckboxChangeHandler)
return;
//...
});
$(this).prop('disabled',true).delay (500).prop('disabled',false)
You can disable and enable element.
$(':checkbox').attr('disabled', 'disabled');
$(':checkbox').removeAttr('disabled');
You can use on
and off
:
$(':checkbox').each(function (k, v) {
$(this).off("change", myFunction)
//do stuff
$(this).on("change", myFunction)
}
function myFunction(e) {
//your event handling code
}
You will also need to use myFunction
when you initially set the handler.
本文标签: javascriptTemporarily deactive onchange eventStack Overflow
版权声明:本文标题:javascript - Temporarily deactive onchange event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741597594a2387504.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论