如何设置多个bootstrapDdatepickers的默认日期

How to set the default date of multiple bootstrap Ddatepickers

我有一个动态生成的 table 输入,其第一列是日期字段。为此,我正在使用 bootstrap-datepicker。每个输入都工作正常,因为我在下面的循环中单独命名它们

for ($r= 1; $r <= $rows; $r++) { 
  $thisDate = '"'.'date'.$r.'"';
     ...
        <input class="datepicker" name= <?php Echo $thisDate; ?> >
     ...
}

但是,如图所示,只有第一项设置了默认值

如何让他们都设置默认日期?

Jquery

$(document).ready(function() {
  $('.datepicker').datepicker({
    "setDate": new Date(),
    todayBtn: true,
    todayHighlight: true,
    autoclose: true,
    orientation: "top"                  
  }).datepicker("setDate", "0");
});

一种解决方案是在调用 datepicker init 之前在输入上设置所需的值:

$(document).ready(function() {
    var oToday = new Date();
    var sToday = oToday.getMonth()+1 + "/" + oToday.getDate() + "/" + oToday.getFullYear();
    $('.datepicker').prop("value", sToday).datepicker({
        autoclose: true
    });
});

以上内容仍然需要对日期进行一些调整以使其格式完全相同,但这是一个要点。在jsfiddle中也是一样的: http://jsfiddle.net/tuG6C/929/

您可以使用 jQuery each() 方法为所有日期选择器设置默认值。

JS:

$(document).ready(function () {
    $('.datepicker').each(function () {
        $(this).datepicker({
            "setDate": new Date(),
            todayBtn: true,
            todayHighlight: true,
            autoclose: true,
            orientation: "top"
        }).datepicker("setDate", "0");
    });
});