jQuery 解除绑定(bind)没有按我的预期工作

标签 jquery

我有这个jquery

$('input#application_submit.bigbutton[value=Agree]').click(function(e){
    e.preventDefault();
    var reason = $('.agree_reason:visible');
    var reason_text = $('.agree_reason:visible textarea');
    if(reason.length == 0){
        $('.agree').show();
    }else{
        if(reason_text.val() == ''){
            $('.agree').show();
            $('#application_agree').css('border', '1px solid #CC0000');
            $('.agree_validation_message').show();
        }else{
            $('input#application_submit.bigbutton[value=Agree]').unbind();
            $('input#application_submit.bigbutton[value=Agree]').click();
        }
    }
});

一切都很好,直到解除绑定(bind)......我需要它做的是删除这个处理程序并执行正常的点击事件,但它卡住了我的浏览器,因为我猜它处于无限循环......我曾经尝试过 jQuery diefundtion,但也没有运气......任何想法

最佳答案

您必须将事件类型传递给取消绑定(bind)

$('input#application_submit.bigbutton[value=Agree]').unbind("click");

此外,根据 PeeHaa 的评论,application_submit 是元素 id,并且由于 dom 中的元素是唯一的(或者至少更好),因此您可以将其简化为

$('#application_submit').unbind("click");

您几乎总是最好通过像这样的简单 ID 进行选择,因为 jQuery 可以在幕后遵循 native document.getElementById

另一个改进是:

var reason = $('.agree_reason:visible');
var reason_text = $('textarea', reason); // it is faster to let jQuery search in context

关于jQuery 解除绑定(bind)没有按我的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8761626/

相关文章:

javascript - 滚动功能使左收缩

javascript - 如何让jquery显示和隐藏图像

javascript - 拖动时 iframe 在 iOS 上移位

javascript - 有没有办法重构这个 javascript/jquery?

c# - ASP.NET MVC 2 + jQuery 灯箱 + 登录

jquery - 当您在其外部单击时,Bootstrap 3 移动导航会崩溃

jquery - 是否有可用于在我的网站中进行视频修剪/剪切的 flash/JavaScript 库?

java - primefaces 部分处理不起作用

c# - 无法定位特定 div 以填充来自 jQuery AJAX ASP.NET 的数据

jQuery Accordion 100% 宽度