本文介绍了在Bootstrap datepicker中将当前日期设置为已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中使用。如何在JavaScript中选择当前日期并将其显示为已选中?

I am using bootstrap-datepicker in my code. How Do I select current date in JavaScript and show it as selected?

经过研究后,这是我正在使用的代码,但当前日期未显示为已选择:

After research this is code I am using but current day does not appear as selected:

var today = moment();

datepicker1.datepicker({
    endDate: today.toDate()
});

datepicker2.datepicker({
    startDate: today .toDate(),
    endDate: today.toDate()
});

datepicker1.datepicker('setDate', today.toDate());
datepicker2.datepicker('setDate', today.toDate());

datepicker1.datepicker('update');  //update the bootstrap datepicker
datepicker2.datepicker('update');


推荐答案

不知道你使用的代码和没有HTML标记没有多大意义,

No idea about the code you are using and without HTML markup doesn't make much sense,

以下是您所追求的实际示例

Here is a working example of what you are after

$(document).ready(function() {
  var date = new Date();
  var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  var end = new Date(date.getFullYear(), date.getMonth(), date.getDate());

  $('#datepicker1').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
  });
  $('#datepicker2').datepicker({
format: "mm/dd/yyyy",
todayHighlight: true,
startDate: today,
endDate: end,
autoclose: true
  });

  $('#datepicker1,#datepicker2').datepicker('setDate', today);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/css/bootstrap-datepicker.min.css" />
<div class="row">
  <div class="col-lg-2">
<div class="panel">
  <fieldset>
    <div class="form-group">
      <label for="datepicker1">Date</label>
      <input type="text" class="form-control" id="datepicker1">
    </div>
  </fieldset>
  <fieldset>
    <div class="form-group">
      <label for="datepicker2">Date2</label>
      <input type="text" class="form-control" id="datepicker2">
    </div>
  </fieldset>
</div>
  </div>
</div>

这篇关于在Bootstrap datepicker中将当前日期设置为已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 10:59