Jquery .live works but not with .datepicker

前端 未结 4 903
梦谈多话
梦谈多话 2020-12-03 04:05

Thanks for looking, all sincerely helpful answers are up-voted. I have some date input fields that are there when the page loads and a bunch that get generated dynamically.

相关标签:
4条回答
  • 2020-12-03 04:37

    This is what I ended up using. It takes advantage of live and focus in newer jQuery

    $.datepicker.setDefaults({ dateFormat: 'yy-mm-dd', ... });
    $('input.date').live('focus', function() {
        $(this).datepicker().datepicker('show');
        true;
    });
    
    0 讨论(0)
  • 2020-12-03 04:52

    Update : As of jQuery 1.7, the .live() method is deprecated. 1.7, Use .on() to attach event handlers. Reference : http://api.jquery.com/live/

    0 讨论(0)
  • 2020-12-03 04:57

    Here is an article about the datepicker using the .live-event in jQuery:

    http://www.vancelucas.com/blog/jquery-ui-datepicker-with-ajax-and-livequery/

    The problem is that the Datepicker works by binding to the focus() event by default, but as of jQuery 1.3.2, the ‘focus’ event cannot be monitored by the ‘live’ event function.

    Here is the work-around from the site::

    <script type="text/javascript">
    $(function(){
        $('input.calendarSelectDate').live('click', function() {
            $(this).datepicker({showOn:'focus'}).focus();
        });
    });
    </script>
    

    EDIT: This workaround is no longer needed as jQuery 1.4.1+ now supports focus and blur events for live(). (Thanks @Chris S)

    0 讨论(0)
  • 2020-12-03 04:57

    Worth noting that jQuery 1.4.1+ now supports focus and blur events for live(), so the workaround, whilst cool, is no longer needed - the orignal poster's version works fine!

    0 讨论(0)
提交回复
热议问题