Bootstrap DatePicker - 将日期设置为明天

时间:2015-03-18 12:16:41

标签: javascript twitter-bootstrap datepicker bootstrap-datepicker

我有一个按钮,使用以下代码将datepicker设置为明天:

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

但它不起作用。

Here's a demo in fiddle

任何想法?

1 个答案:

答案 0 :(得分:5)

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

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

Stack Snippets中的演示



$('#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>
&#13;
&#13;
&#13;