How to remove all click event handlers using jQuery?

后端 未结 5 1915
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 02:18

I\'m having a problem. Basically, when a user clicks an \'Edit\' link on a page, the following Jquery code runs:

$(\"#saveBtn\").click(function () {
    save         


        
相关标签:
5条回答
  • 2020-12-01 02:55

    If you used...

    $(function(){
        function myFunc() {
            // ... do something ...
        };
        $('#saveBtn').click(myFunc);
    });
    

    ... then it will be easier to unbind later.

    0 讨论(0)
  • 2020-12-01 02:59
    $('#saveBtn').off('click').click(function(){saveQuestion(id)});
    
    0 讨论(0)
  • 2020-12-01 03:12

    Is there a way to remove all previous click events that have been assigned to a button?

    $('#saveBtn').unbind('click').click(function(){saveQuestion(id)});
    
    0 讨论(0)
  • 2020-12-01 03:13
    $('#saveBtn').off('click').on('click',function(){
       saveQuestion(id)
    });
    

    Use jquery's off and on

    0 讨论(0)
  • 2020-12-01 03:15

    You would use off() to remove an event like so:

    $("#saveBtn").off("click");
    

    but this will remove all click events bound to this element. If the function with SaveQuestion is the only event bound then the above will do it. If not do the following:

    $("#saveBtn").off("click").click(function() { saveQuestion(id); });
    
    0 讨论(0)
提交回复
热议问题