Bootstrap 日期选择器手动设置日期不起作用

Bootstrap datepicker set date manually not working

我有一个遗留网络应用程序正在使用一些旧版本的 bootstrap datepicker。我已经在此处上传了整个 datepicker.js 文件。 https://pastebin.com/aYzMrWgW

我正在尝试设置我的开始日期选择器,以便当它发生变化时,我得到所选的值并计算所选同月的最后一天,并将该值设置为结束日期的值。

我正在使用下面的代码从开始日期选择器 $('#dp1') 获取值并尝试将其设置为结束日期选择器 $('#dp1a') 的值。当我检查控制台时,我的 lastDay 变量是正确的。如果我选择 8/1/2017 - 它会注销 8/31/2017 及其后的字符串表示形式。但是我无法用正确的值更新结束日期。

$('#dp1').datepicker().on('changeDate', function(ev) {
  $('.datePickerStart').change();
});

$('.datePickerStart').change(function() {
  // Parse the date and get the last day of the month selected
  var date = new Date($(this).val());
  var lastDay = new Date(date.getFullYear(), date.getMonth() + 2, 0);
  console.log(lastDay);


  $('#dp1a').datepicker('setValue', lastDay);
});

如果我像上面那样调用 setValue,它会将值设置为 9/1/2017。

谁能指出我做错了什么?

您的 bootstrap 日期选择器版本太旧,似乎没有完整的方法 .datepicker('setValue', value)。与当前的 version.You 相比,缺少一些代码行检查差异 here。 即使我使用您的版本对您的代码进行了一些更改以设置值。

$(document).ready(function(){
var second = $('#dp1a').datepicker().data('datepicker');
$('#dp1').datepicker().on('changeDate', function(ev) {
  $('.datePickerStart').change();
});

$('.datePickerStart').change(function() {
  // Parse the date and get the last day of the month selected
  var date = new Date($(this).val());
  var lastDay = new Date(date.getFullYear(), date.getMonth() + 2, 0);
  console.log(lastDay);
  second.date=lastDay;
    second.setValue();
});
});

old version fiddle

但是您的代码在使用当前 version.Its 更新后可以完美运行,最好使用最新版本以轻松利用这些方法。

current version fiddle

希望对您有所帮助!