简体   繁体   中英

Jquery off('click') on('click') bind and unbind

I have some buttons (Datatable button to export data) I need to prevent direct data download for that I am implementing OTP so when user first click on button its hows user a dialog box where he/she need to put OTP then if OTP matches then I need to remove .off("click"); method so that buttons can work again. on document ready I add this event like below

$(".dt-buttons button").each(function(){
    $(this).off("click");
});

Now how can i remove this .off("click"); so buttons can work again like default

I suggest the use of common class with event delegation on() instead of detaching/attaching the event every time, you could give your button a common class example click_event and remove/add class as you want like :

$(".dt-buttons button").each(function(){
    $(this).removeClass("click_event");
});

//When you want to attach the event 
$('your_selector').addClass("click_event"); 

Hopefully this will attach the normal behavior again on your element

             $("Your_selectors").on('click', function(){
                    $(this).trigger('click');
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM