Unbind jQuery even handler

前端 未结 6 762
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 04:38

I try to answer this question a few minutes ago and prepared this example for myself :



        
6条回答
  •  天命终不由人
    2020-12-19 05:12

    No one should ever mix their markup with their interaction code if they are using jQuery.

    Add some javascript to the page like this:

    $(function() {
        $('#aspnetForm').bind('submit',function() {
            trialMethod();
        });
        $('#btnTrial2').bind('click',function() {
            $('#aspnetForm').unbind('submit');
        });
        $('#btnTrial2').bind('click',function() {
            $('#aspnetForm').bind('submit', trialMethod2).unbind('submit');
        });
    });
    

    Now, with that out of the way... Everything should work now (even though you will now be double-binding the #aspnetForm before unbinding it completely when the second button is pressed). The problem was that the form was never really 'bound' to begin with. You can unbind onsubmit parameters in the markup.

提交回复
热议问题