Bootstrap-Datepicker not selecting date when using "set

Bootstrap-Datepicker not selecting date when using "setDate"

本文关键字:quot set using when not selecting date Bootstrap-Datepicker      更新时间:2023-09-26

我正在尝试让Bootstarp Datepicker工作,我已经非常接近了。然而,现在我已经找到了如何设置startDate,并且能够让"自动关闭"工作,我无法让它选择在"setDate"中设置的日期

如果我调出日历,然后手动选择另一个将显示为选定的日期,但setDate值不会显示为选定日期。我做了很多搜索,但似乎找不到合适的组合。

顺便说一句:在F12工具中,我确实有一个错误,但它说"加载资源失败:服务器响应404(找不到)",源指向"/fonts/glyphiconshallings-regular"文件。我不认为这与此有关,但我将为此发布一个单独的问题。

这是我的HTML

<div class="col-md-2 text-left">
    <div id="StartDate" class="input-group date">
        <input class="datepicker form-control" type="text" />
        <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
    </div>
</div>
<div class="col-md-2">
    <div id="EndDate" class="input-group date">
        <input type="text" class="form-control">
        <div class="input-group-addon">
            <span class="glyphicon glyphicon-th"></span>
        </div>
    </div>
</div>

这是我的JS。这个JS会设置日期并使自动关闭工作,但它不会选择用setDate方法设置的日期。

<script type="text/javascript">
    $(function () {
        var startDate = new Date();
        startDate.setDate(startDate.getDate() - 7);
        var endDate = new Date();
        $("#StartDate").datepicker({
            autoclose: true
        }).datepicker("update", startDate);
        $("#EndDate").datepicker({
            autoclose: true
        }).datepicker("update", endDate);
    });
</script>

如果我切换到这个JS代码,我可以让setDate工作,也可以得到设置为选择的日期。但是,这会导致自动关闭无法工作。

<script type="text/javascript">
    $(function () {
        var startDate = new Date();
        startDate.setDate(startDate.getDate() - 7);
        var endDate = new Date();
        $("#StartDate").datepicker("setDate", startDate);
        $("#StartDate").datepicker("autoclose", true);
        $("#StartDate").datepicker("update");
    });
</script>

AutoClose不工作时,可以使用以下函数强制执行:

$('#StartDate').datepicker().on('changeDate', function(ev){
    $('#StartDate').datepicker('hide');
});

$(function () {
        var startDate = new Date();
        startDate.setDate(startDate.getDate() - 7);
        var endDate = new Date();
        $("#StartDate").datepicker("setDate", startDate);
        
        $("#StartDate").datepicker("update");
        
        $('#StartDate').datepicker().on('changeDate', function(ev){  
            $('#StartDate').datepicker('hide');
        });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/datepicker.css">
<div class="col-md-2 text-left">
    <div id="StartDate" class="input-group date">
        <input class="datepicker form-control" type="text" />
        <span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
    </div>
</div>