jQuery event handler .on() not working

前端 未结 6 1245
小鲜肉
小鲜肉 2020-12-08 09:49

I want to attach a event to dynamically created element class.So i used live function but it was not triggered. So checked live function reference ,there i red below notes

6条回答
  •  醉话见心
    2020-12-08 10:29

    This is tricky.

    When your code runs, your element does not have .date_picker_disabled class so your jQuery(".date_picker_disabled") returns nothing and .on() is not called.

    Apply .on() on the outer element and use the selector parameter:

    // you can also do $(document).on()
    $().on('click', '.date_picker_disabled', function() {
        // do something
    });
    

    This will delegate the event to the . The handler will only be executed if an element with class .date_picker_disabled has been clicked (second param).

提交回复
热议问题