Weird bug when combining an ASP.NET updatepanel with the jQuery UI DatePicker

前端 未结 10 762
时光说笑
时光说笑 2021-02-04 15:42

I\'ve created a page which combines an inline jQuery UI datepicker . I wan\'t to initiate a callback to the updatepanel when the user clicks a new date to update some data. Now,

10条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 16:34

    A common problem with combinations of AJAX and jQuery is that this...

    $(document).ready(function() {
           buildDatepicker();
    

    Only happens on the first page load. If you replace the area of HTML that has been affected by buildDatapicker(); you lost any events that were attached to it (even if you've replaced it with the same elements).

    All you need to do is call buildDatepicker against the newly loaded HTML... and pass in the element that has been loaded just to ensure you don't double up...

    $(document).ready(function() {
           buildDatepicker(document);
    ...
    
    function buildDatepicker(element) {
        var dp = $("#datepicker", element).datepicker({
            onSelect: function(dateText, inst) {
                /* Do a postback when someone clicks a date */
                doAspNetPostback();
            }
        });
    }    
    

    Now you can call buildDatepicker(myNewlyLoadedElement); to re-bind the data picker functionality.

提交回复
热议问题