如何将今天设置为我的日期选择器默认值?

时间:2018-12-29 14:24:49

标签: jquery twitter-bootstrap datepicker

我想将日期选择器的默认值设置为今天:

  $(document).ready(function() {
     $('.js-date').datepicker({
          "setDate": new Date(),
          "autoclose": true
     });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" rel="stylesheet"/>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.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/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>

<input type="text" class="js-date form-control" />

1 个答案:

答案 0 :(得分:1)

您可以这样做:

$('.js-date').datepicker({
  "autoclose": true
}).datepicker("setDate",'now');

工作演示

$(document).ready(function() {
  $('.js-date').datepicker({
    "autoclose": true
  }).datepicker("setDate",'now');
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.css" rel="stylesheet" />
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.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/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>

<input type="text" class="js-date form-control" />