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

后端 未结 4 1303
有刺的猬
有刺的猬 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 ();
    }
    

提交回复
热议问题