Bootstrap DatePicker - 将日期设置为明天

Bootstrap DatePicker - Setting the date to Tomorrow

我有一个按钮可以使用以下代码将日期选择器设置为明天:

$("#txtDateOfJourney").datepicker({ startDate: '+1d'});

但是没用。

Here's a demo in fiddle

有什么想法吗?

您只在初始化控制器时传入选项(如 datepicker({ startDate: '+1d'}))。由于它已经初始化,因此您不需要这样做。相反,您想使用 setDate() method and then pass in the javascript date representation of tomorrow 表示相对日期的字符串,如下所示:

$('#txtDateOfJourney').datepicker('setDate', "+1d");

堆栈片段中的演示

$('#btnTomorrow').click(function() {     
  $('#txtDateOfJourney').datepicker('setDate', "+1d");
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.js"></script>

<input type="text" id="txtDateOfJourney" class="datepicker" />
<button type="button" id="btnTomorrow" class="btn btn-info">Tomorrow</button>