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
Add a ment  | 

4 Answers 4

Reset to default 7

I 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