简体   繁体   English

Datepicker在Ajax加载的div中不起作用

[英]Datepicker not working in div loaded by ajax

I have a div which is populated by ajax. 我有一个由ajax填充的div。 When done this way the datepicker is not called. 完成这种方式后,不会调用datepicker。 I originally had the datepicker loaded in the header of the containing page but have tgried loading it within the script which calls the div content but again no luck. 我最初将datepicker加载到包含页面的页眉中,但在调用div内容的脚本中尝试将其加载到网格中,但还是没有运气。 The Jquery library is called in the header of the main page. 在首页的标题中调用Jquery库。 If I load the pages without ajax then there is no problem and the datepicker works fine. 如果我加载的页面没有ajax,则没有问题,datepicker可以正常工作。

<li><a data-target="#halloffame" href="abscencerecords.php">Absence Records</a></li>
<li><a data-target="#halloffame" href="abscencerequestform.php">Absence Request</a></li>
<li><a data-target="#halloffame" href="personaldetails.php">Personal Details</a></li>
        <?php endif ?>
        <?php endif ?>
        </ul></div>
        <div class="clear padding20"></div>
<div id="halloffame" class="bordered_box"></div>
        <script>
  $('[data-target]').click( function (e) {
   $(document).ready(function() { $("#datepicker").datepicker({ dateFormat: 'dd/mm/yy' }); }); 
   $(document).ready(function() { $(".datepickermultiple").multiDatesPicker({ dateFormat: 'dd/mm/yy' }); });
    var target = $($(this).attr('data-target')); 
   target.load($(this).attr('href'));
    e.preventDefault(); // prevent anchor from changing window.location
  });
</script>

Most plugins only initialize for elements that exist ... at the time the code is run 大多数插件仅针对存在的元素进行初始化……在运行代码时

In order to initialize a plugin within ajax loaded content you need to do that within the completion callback of ajax. 为了在ajax加载的内容中初始化插件,您需要在ajax的完成回调中进行。

Try changing : 尝试更改:

target.load($(this).attr('href'));

To

target.load($(this).attr('href'), function(){
    // new html now exists , initialize plugins or event handlers or dom manipulation here
    $(this).find(selector).somePlugin()
});

Reference load() docs 参考load()文档

In my case datepicker plugin was getting initialize, but the issue was position of datepicker calendar, I solved it by adding below CSS to the page: 在我的情况下,datepicker插件正在初始化,但是问题是datepicker日历的位置,我通过在页面中添加以下CSS来解决了它:

#ui-datepicker-div {
    position: absolute !important;;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM