Bootstrap datepicker setDatesDisabled无法单击按钮

时间:2016-04-01 17:04:47

标签: javascript jquery twitter-bootstrap

尝试在按钮上单击setDatesDisabled以获取bootstrap datepicker,但它无效。

HTML:

<div id="container">
  <div id="date-range">
    <div class="range-start"></div>
    <div class="range-end"></div>
  </div>
  <button id="button" class="btn btn-warning">Disable Dates</button>
</div>

JavaScript的:

$(function() {
  $('#date-range').datepicker({
    inputs: $('.range-start, .range-end'),
    format: 'yyyy-mm-dd',
    datesDisabled: ['2016-04-21', '2016-04-20', '2016-04-13']
  });

  $('#button').click(function() {
    $('#date-range').datepicker('setDatesDisabled', ['2016-04-29', '2016-04-30']);
  });
});

小提琴:https://jsfiddle.net/kaygee79/953zk742/

1 个答案:

答案 0 :(得分:4)

初始化datepicker时使用指定的inputs

$(function() {
  $('#date-range').datepicker({
    inputs: $('.range-start, .range-end'),
    format: 'yyyy-mm-dd',
    datesDisabled: ['2016-04-21', '2016-04-20', '2016-04-13']
  });

  $('#button').click(function() {
    $('.range-start').datepicker('setDatesDisabled', ['2016-04-29', '2016-04-30']);
    $('.range-end').datepicker('setDatesDisabled', ['2016-04-29', '2016-04-30']);
  });
});

工作小提琴: https://jsfiddle.net/f50ga49j/