How to bind jQuery Datepicker after Ajax refresh?

前端 未结 4 602
误落风尘
误落风尘 2021-01-14 04:44

I\'ve got a jQuery datepicker that is working great for me, except when I fresh the content via ajax, I lose the datepicker. From what I can tell I should be using jQuery on

4条回答
  •  臣服心动
    2021-01-14 05:33

    Actually, the simplest way I have found to do this is by simply putting the bind code within the success function like so: ELEMENT.datepicker();.

    Eg: $('.datepicker').datepicker();

    Full code:

    $('#loaddetails').on('click',function(){
                    //Run ajax fetch here
                        $.ajax({
                           type: "POST",
                           url: "yourlink",
                           data: $('form').serialize(), // serializes the form's elements.
                           dataType: 'json',
                           beforeSend: function () {
                             console.log('Form serialised');
                             $('#status').html('');
                             },
                           success: function(data)
                           {
                            $('#status').html(data);
                            $('.datepicker').datepicker();
                              },
                              error: function (xhr, ajaxOptions, thrownError) {
                                alert(xhr.status);
                                alert(thrownError);
                              }
                         });
                    console.log("All ok");
                    return false;
                });
    

提交回复
热议问题