How to run a callback function on a jQuery trigger(“click”)?

后端 未结 8 1900
太阳男子
太阳男子 2021-01-03 18:44

I need to trigger a custom event in the callback of a trigger call, but I can\'t get it to work.

I tried this:

var $input = $( \".ui-pop         


        
相关标签:
8条回答
  • 2021-01-03 19:23

    First you need to bind the click event and then you can trigger the click event.

    $input.bind('click', function() {
    
         console.log("clicked the input");
    });
    
    $input.trigger('click');
    
    0 讨论(0)
  • 2021-01-03 19:26

    Yes - It is true that trigger doesn't take callback but we can pass callback as parameter.

    //.trigger( eventType [, extraParameters ] )
    
    $("#element").bind("customCall", function(e, callback){
       callback();
    }
    var callback = function(){alert("Hello");}
    $("#element").trigger("customCall",[callback]);
    

    Hope this will helps

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