简体   繁体   English

Bootstrap Datepicker无法正常工作

[英]Bootstrap Datepicker is not working properly

I have implemented bootstrap daterangepicker in my script, It is working fine at first time. 我已经在脚本中实现了bootstrap daterangepicker,它在第一次运行时就很好。 If I click the link again (in which I have implemented the daterangepicker and loads the page content using AJAX call), the bootstrap daterangepicker is not working. 如果再次单击链接(在其中实现了daterangepicker,并使用AJAX调用加载了页面内容),则引导daterangepicker无法正常工作。

I am loading a page body content using ajax. 我正在使用Ajax加载页面正文内容。 I have included the daterange picker scripts and include files in a page body content. 我已经包括了日期范围选择器脚本,并在页面正文内容中包括了文件。

I have implemented the daterangepicker inside a JQuery ready() function. 我已经在JQuery ready()函数内部实现了daterangepicker。

This is my code, which I have implemented. 这是我已实现的代码。

HTML code: HTML代码:

     <div id="reportrange" class="pull-right" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc">
           <i class="icon-calendar icon-large"></i>
           <span></span> <b class="caret" style="margin-top: 8px"></b>
      </div>

JS Code: JS代码:

 $(document).ready(function() {

       $('#reportrange').daterangepicker({
                    ranges: {
                       'Today': [new Date(), new Date()],
                       'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
                       'Last 7 Days': [moment().subtract('days', 6), new Date()],
                       'Last 30 Days': [moment().subtract('days', 29), new Date()],
                       'This Month': [moment().startOf('month'), moment().endOf('month')],
                       'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
                    },                        
                    format: 'MM/DD/YYYY',
                    separator: ' to ',
                    startDate: moment().subtract('days', 6),

                 },
                 function(start, end) {
                    $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                    start = start.format('MMMM D, YYYY');
                    end = end.format('MMMM D, YYYY');
                    test(start,end);

                 }
              );
      $('#reportrange span').html(moment().subtract('days', 6).format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));                  

}); });

Please help me to solve my problem. 请帮助我解决我的问题。

jQuery's document ready function fires as soon as the DOM content for the page loads - this happens usually just before rendering but almost always before any AJAX calls to the server can load any of your content, including your reportrange div. jQuery的文档就绪功能会在页面的DOM内容加载后立即触发-这种情况通常发生在渲染之前,但几乎总是在对服务器的任何AJAX调用都可以加载您的任何内容之前,包括reportrange div。 This means that once your ajax call with the reportrange html loads, you would need to fire that $('#reportrange').daterangepicker({ stuff again. 这意味着,一旦使用reportrange html加载了ajax调用,您将需要触发$('#reportrange').daterangepicker({

Luckily, jQuery knows we sometimes need to do this, so the question is where you want to put it. 幸运的是,jQuery知道我们有时需要执行此操作,因此问题是您要将其放置在何处。

If you load your reportrange div in an explicit way, something like 如果您以显式方式加载reportrange div,则类似于

$(".rangeArea").load("reportRange");

then jQuery gives you the option of a callback: 然后jQuery为您提供了回调选项:

$(".rangeArea").load("reportRange", function() {
    $('#reportrange').daterangepicker({
        // ... your picker code here ...
    });
});

If you load your ajax area in some other way, you may want to wire your picker code up through the global ajax complete handler - this fires after every ajax call, so do be careful if you have a lot of back & forth communication: 如果您以其他方式加载ajax区域,则可能需要通过全局ajax完整处理程序来连接选择器代码-这在每次 ajax调用后都会触发,因此如果您进行了很多来回通信,请务必小心:

$.ajaxComplete(function(){
    $('#reportrange').daterangepicker({
        // ... your picker code here ...
    });
});

Another thing that might bail you out is that you are calling your <div id=reportrange> which means there can only be one of them on the page - remember that id attributes must be unique on a page, even if content is loaded later. 可能会让您烦恼的另一件事是,您正在调用<div id=reportrange> ,这意味着页面上只能有一个-记住即使在以后加载内容时,id属性在页面上也必须是唯一的。 If you might ever have a second reportrange on the same page, you will probably want to switch from IDs to class names: <div class=reportrange> and $(".reportrange") . 如果您也许有第二个reportrange在同一页上,你可能会想从标识切换到类名称: <div class=reportrange>$(".reportrange")

Final thing - jQuery(document).ready(function(){...}) can be written more succinctly like $(function(){...}) 最后一件事jQuery(document).ready(function(){...})可以像$(function(){...})一样简洁地编写

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

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