Use jQuery .trigger to call a custom function that returns a value

后端 未结 4 1296
有刺的猬
有刺的猬 2021-02-14 04:34

Is there any way using jQuery\'s .bind() and .trigger() calls to execute a user defined function (ex: save ()) and act upon the return from the method? For example:

<         


        
4条回答
  •  情书的邮戳
    2021-02-14 05:00

    I know this question is old, but maybe this can help someone.

    As Paliath pointed, you can't return a value because the trigger event is asynchronous. What you can do, is pass a object as parameter to the event. By default, javascript objects are always references to the original.

    eg.:

    bind:

    $(...).bind('example',function (evt,ret) { 
        ret.val = false;
    });
    

    trigger:

    var ret = {ret:true};
    $(...).trigger('example',[ret]);
    if (!ret.ret)
        //event return false
    else
        //event returned true
    

提交回复
热议问题