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

后端 未结 8 1965
太阳男子
太阳男子 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:08

    .trigger() does not accept any callbacks. You don't need it most cases because the bound event handler is immediately executed. However, if you have an AJAX call in trigger event handler, the code from accepted answer will not work:

    $input.trigger('click');
    runtests();
    

    You will have to use setTimeout() like this:

    $input.trigger('click');
    setTimeout(function () {
        runtests();
    }, 250);
    

    Or consider using success or error callbacks for .ajax() call in .trigger() event handler.

提交回复
热议问题