简体   繁体   中英

jQuery bind function doesn't work

I seem to have problem with jQuery bind() and unbind() functions. They seem to not get executed at all. Here's the code I have so far:

$(function() {
    var init = function() {
        $.get('test.php', function(j) {
            if(j.r && j.r == 'error') {
                console.log("1");
                $('#far').bind('click', function() {
                    console.log("2"); return false;
                });
            } else {
                //getData();
            }
        })
    }();

    var getData = function() {
        $('#far').unbind('click', false);
    };

    $('#far').click(function() { ... });
});

Console only shows me 1 and no errors. I have no idea what I'm doing wrong since there are no errors and jQuery docs and StackOverflow has given me the same answer. I'm using jQuery 1.4.4

What am I doing wrong?

By calling your getData() function at the end of the $.get() function you are unbinding the event as soon as you bind it. Try commenting out the getData() call in the $.get() function and see if that helps.

如我所见,您在创建事件之前就绑定了该事件,因此请使用live代替。

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