Jquery 从$(document).ready()调用时未激发自定义事件;

Jquery 从$(document).ready()调用时未激发自定义事件;,jquery,custom-events,jquery-trigger,jquery-triggerhandler,Jquery,Custom Events,Jquery Trigger,Jquery Triggerhandler,我有以下代码 $(document).ready(function () { VerifyCustomerFilter(); $("#ToDateStor").on("DateSet", function () { ... ); function VerifyCustomerFilter() { if(toDate != "") $("#ToDateStor").trigger('DateSet'); //Thi

我有以下代码

$(document).ready(function () {

    VerifyCustomerFilter();

    $("#ToDateStor").on("DateSet", function () {
       ...
    );

   function VerifyCustomerFilter() {
     if(toDate != "")     
       $("#ToDateStor").trigger('DateSet'); //This does not work
   }
}
当函数“VerifyCustomerFilter()”中的条件为true时,无法触发我创建的自定义事件

但是当我在我的日期选择器的事件中触发事件时,它工作得非常好:看到了吗

 $("#calTo").datepicker({
      showButtonPanel: false,
      onClose: function (dateText, inst) {
          var ToDate = $(this).val().toString();
          $('#ToDateStor').html(ToDate).trigger('DateSet'); // This works!
      }
 });
也已尝试使用
triggerHandler()


我做错了什么?

在将侦听器绑定到元素之前,如果正在触发事件,请重试

$(document).ready(function () {
    // add the listener
    $("#ToDateStor").on("DateSet", function () {
       ...
    });

    // define the function
    function VerifyCustomerFilter() {
        if(toDate != "")     
            $("#ToDateStor").trigger('DateSet'); //This does not work
        }
     }

     // call the function
     VerifyCustomerFilter();
});

如果要在将侦听器绑定到元素之前触发事件,请重试

$(document).ready(function () {
    // add the listener
    $("#ToDateStor").on("DateSet", function () {
       ...
    });

    // define the function
    function VerifyCustomerFilter() {
        if(toDate != "")     
            $("#ToDateStor").trigger('DateSet'); //This does not work
        }
     }

     // call the function
     VerifyCustomerFilter();
});
在绑定事件之前,您正在调用函数


您在绑定事件之前调用了函数。

在绑定事件之前触发了事件listener@atmd您应该回答这个问题。在绑定listener@atmd您应该回答这个问题。
trigger()
不会绑定事件,
。on()
会绑定事件,所以请检查您的answer@empiric那太尴尬了。感谢您指出。
trigger()
不绑定事件,
.on()
会绑定事件,因此请检查您的answer@empiric那太尴尬了。谢谢你指出。是的,我把事情的顺序搞混了,现在一切都好了。谢谢你抽出时间!是的,我把事情的顺序搞混了,现在一切都好了。谢谢你抽出时间!