DatePicker Not working On Ajax Loaded Content

2019-05-14 16:49发布

I have stucked with this problem. The datepicker is working fine on static content.

But fails when content is loaded via Ajax.

I Have tried :-

jQuery.noConflict();
jQuery(function($) {
    $(document).ready(function(){
        $( ".datepickerRange" ).datepicker({
            dateFormat: 'dd-mm-yy',
            prevText:'',
            nextText:'',
            minDate: 0,
            maxDate: "+1M",
            showOn: "button",
            buttonImage: divadatepicker.image_url+"/calendar.jpeg",
            buttonImageOnly: true
        });
        $( ".datepicker" ).datepicker({
            dateFormat: 'dd-mm-yy',
            prevText:'',
            nextText:'',
            showOn: "button",
            buttonImage: divadatepicker.image_url+"/calendar.jpeg",
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            yearRange: "1960:2015"
        });

            });

$(document).live(function(){
        $( ".datepickerRange" ).datepicker({
            dateFormat: 'dd-mm-yy',
            prevText:'',
            nextText:'',
            minDate: 0,
            maxDate: "+1M",
            showOn: "button",
            buttonImage: divadatepicker.image_url+"/calendar.jpeg",
            buttonImageOnly: true
        });
        $( ".datepicker" ).datepicker({
            dateFormat: 'dd-mm-yy',
            prevText:'',
            nextText:'',
            showOn: "button",
            buttonImage: divadatepicker.image_url+"/calendar.jpeg",
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            yearRange: "1960:2015"
        });

            });

});

It is not showing the even calender small image after content loaded via ajax.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-14 17:07

Call .datepicker in your success callback function on your ajax call instead of using .live, which is deprecated. Example:

function initdatepicker(){
    $( ".datepickerRange" ).datepicker({
        dateFormat: 'dd-mm-yy',
        prevText:'',
        nextText:'',
        minDate: 0,
        maxDate: "+1M",
        showOn: "button",
        buttonImage: divadatepicker.image_url+"/calendar.jpeg",
        buttonImageOnly: true
    });
    $( ".datepicker" ).datepicker({
        dateFormat: 'dd-mm-yy',
        prevText:'',
        nextText:'',
        showOn: "button",
        buttonImage: divadatepicker.image_url+"/calendar.jpeg",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        yearRange: "1960:2015"
    });
}
$(document).ready(function(){
    initdatepicker();
});

And add this to your $.ajax success:

initdatepicker();
查看更多
一夜七次
3楼-- · 2019-05-14 17:11

Use this

<script type="text/javascript">
    $(function(){
        $('input.calendarSelectDate').live('click', function() {
           $(this).datepicker({showOn:'focus'}).focus();
        });
     });
</script>
查看更多
登录 后发表回答