Bootstrap datetimepicker在初始化后禁用几天

时间:2017-06-27 13:52:19

标签: twitter-bootstrap eonasdan-datetimepicker

我尝试在初始化datetimepicker后更改禁用天数。

我使用ajax调用执行此操作,但我尝试简化代码。 我试过$('#datetimepicker').data("DateTimePicker").FUNCTION() 对于FUNCTION,我尝试了.disableddates.setDisabledDates

第一个删除了之前宣布的残疾人日期,但没有添加新日期。

<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker-standalone.css" >
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js" ></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js" type="text/javascript" ></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/nl.js"></script>
    <script src="/Scripts/bootstrap-datetimepicker.min.js" ></script>
  </head>
  <body>

    <div class="container">
      <div class="row">
        <div class='col-sm-6'>
          <div class="form-group">
            <div class='input-group date' id='datetimepicker1'>
              <input type='text' class="form-control" />
              <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
              </span>
            </div>
          </div>
        </div>
      </div>

      <div class="row">
        <button class="btn button-default" onclick="ChangeDate()" >Change disabled days</button>
      </div>
    </div>

    <script type="text/javascript">
      $(function () {
        $('#datetimepicker1').datetimepicker({
          locale:"nl",
          defaultDate: "2017-06-25",
          disabledDates: ['2017-06-26','2017-06-27','2017-06-28'],
          debug: false                    
        });
      });

      function ChangeDate(){
        alert('ChangeDate geklikt !');
        $('#datetimepicker1').data("DateTimePicker").disabledDates(['2017-06-21','2017-06-22']);
        alert('functie is uitgevoerd !');
      }
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

正如文档所述disabledDates

  

采用[ stringDatemoment ]个值,并禁止用户选择那些日期。

如果您传递strings数组,则每个元素应与datetimepicker format选项兼容(在您的情况下,L LTDD-MM-YYYY HH:mm nl } locale)。

问题是datepicker disabledDates函数执行:

moment('2017-06-21', 'DD-MM-YYYY HH:mm')

将输入字符串解析为2021-06-20(已禁用,而不是2017-06-21)。

您可以使用moment(String)解析函数将数组字符串转换为时刻对象数组(请注意,您的输入字符串符合ISO 8601标准格式)。

这是一份工作样本:

$('#datetimepicker1').datetimepicker({
  locale:"nl",
  defaultDate: "2017-06-25",
  disabledDates: ['2017-06-26','2017-06-27','2017-06-28'],
  debug: false
});

function ChangeDate(){
  //alert('ChangeDate geklikt !');
  $('#datetimepicker1').data("DateTimePicker").disabledDates([
    moment('2017-06-21'),
    moment('2017-06-22')
  ]);
  //alert('functie is uitgevoerd !');
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js" ></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js" type="text/javascript" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/nl.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

<div class="container">
  <div class="row">
    <div class='col-sm-6'>
      <div class="form-group">
        <div class='input-group date' id='datetimepicker1'>
          <input type='text' class="form-control" />
          <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
          </span>
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <button class="btn button-default" onclick="ChangeDate()" >Change disabled days</button>
  </div>
</div>

解决此问题的另一种方法是在datetimepicker配置中添加extraFormats: ['YYYY-MM-DD']选项,告诉组件如何正确解析'2017-06-21''2017-06-22'等字符串。