jquery link tag enable disable

后端 未结 3 433
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 04:17

I want to disable the link during loading, for the code given below

\"<%= f.add_associated_link(\'Add Task\', @project.tasks.bu         


        
相关标签:
3条回答
  • 2020-12-16 04:29
    function disableLink(e) {
        // cancels the event
        e.preventDefault();
    
        return false;
    }
    

    When you want to disable it yo call

    $('#addlink').bind('click', disableLink);
    

    When you want to enable disabled link you call

    $('#addlink').unbind('click', disableLink);
    
    0 讨论(0)
  • 2020-12-16 04:35

    I'd go with a hybrid of RaYell's and phoenix's solution, adding jQuery's namespacing to the mix:

    $('#addlink').bind('click.killlink',function(event){
        event.preventDefault();
        // You can do any additional onClick behavior here
    });
    

    To unbind this event, as well as any other related events (of any type) that you group with the .killink namespace, you'd run this:

    $('#addlink').unbind('.killlink');
    

    As phoenix pointed out, using return false will prevent the event from bubbling up. preventDefault() has the added benefit of being extremely explicit (unlike return false, which can mean many different things depending on the context).

    0 讨论(0)
  • 2020-12-16 04:45
    $('#addlink').click(function(e) {
        e.preventDefault();
        //do other stuff when a click happens
    });
    

    return false;

    will prevent the default event from occuring and and also prevent the event from bubbling up

    So chosing between these two depends on your use. If you want to stop the default action and also need to bubble up the event then use preventDefault

    0 讨论(0)
提交回复
热议问题