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

后端 未结 4 1295
有刺的猬
有刺的猬 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 04:40

    You can also use jQuery builtin event.preventDefault() and event.isDefaultPrevented():

    $("#aForm").bind ('save', function (event) {
        return true;
        // "return false" or "event.preventDefault()" have the same effect
      }
    });
    

    and then:

    var event = $.Event('save')
    $("#aForm").trigger (event)
    if (event.isDefaultPrevented() == false) {
      doSomething ();
    }
    
    0 讨论(0)
  • 2021-02-14 04:41

    Check out triggerHandler(), which in many other ways acts like trigger() but will allow you to return a value.

    From the docs:

    The .triggerHandler() method behaves similarly to .trigger(), with the following exceptions:

    • The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission).
    • While .trigger() will operate on all elements matched by the jQuery object, .triggerHandler() only affects the first matched element.
    • Events created with .triggerHandler() do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing.
    • Instead of returning the jQuery object (to allow chaining), .triggerHandler() returns whatever value was returned by the last handler it caused to be executed. If no handlers are triggered, it returns undefined
    0 讨论(0)
  • 2021-02-14 04:43

    Why don't you act on the return value in the callback itself? It takes away the extra check (that won't work like you would expect it to).

    $("#aForm").bind ('save', function () {
      if(some stuff happens that is true) {
          doSomething();
      }
    
      else {
          doSomethingElse();
      }
    });
    

    Callbacks don't work synchronously. They are used to handle things that happen asynchronously (like events). In they context of callbacks, they are pieces of code that run when something happens. Returning values from them really doesn't make sense. In your example, since you want to check on the return value of the callback, you can be sure that you only get a return value when the callback runs (because you're trying to use them like a regular function). In that case, put that logic inside the callback itself (like I have shown above).

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题