简体   繁体   English

jquery unbind来自事件处理程序的事件处理程序

[英]jquery unbind event handlers from an event handler

i have this code 我有这个代码

// define event handlers
$("a.ajax").click(function(event) {
    event.preventDefault();

    var $el = $(this); 
    $.get($el.prop("href"), function(response) {
       $el.trigger("success.aliAjax", [$el, response]); 
    });
});

// define event handlers
var Handlers = function () {
};
Handlers.prototype.redirect = function (event, $el, response) {
    if (response && response.location) {
    //    now i have to redirect and disable "Handlers.prototype.success" event handler
        window.location.href = response.location;
    }
};
Handlers.prototype.success = function (event, $el, response) {
    alert("success");
};

// listen for events
$(document).on('success.aliAjax', Handlers.prototype.redirect);
$(document).on('success.aliAjax', Handlers.prototype.success);

so, my problem is how can i disable "Handlers.prototype.success" event handler in "Handlers.prototype.redirect" event handler if the conditions provided 所以,我的问题是我如何在“Handlers.prototype.redirect”事件处理程序中禁用“Handlers.prototype.success”事件处理程序,如果提供的条件

here is jsfiddle 这是jsfiddle

You can use 您可以使用

$(document).off('success.aliAjax');

Change $("a.ajax").click(... into $("a.ajax").on('click', function... then you can use $("a.ajax").off("click") 更改$("a.ajax").click(...进入$("a.ajax").on('click', function...然后你可以使用$("a.ajax").off("click")

EDIT: 编辑:

Is that what you want? 那是你要的吗? FIDDLE 小提琴

Try using .off() with the handler. 尝试将.off()与处理程序一起使用。 Like so: 像这样:

$(document).off('success.aliAjax', Handlers.prototype.redirect);
$(document).off('success.aliAjax', Handlers.prototype.success);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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