无法从 bootStrap 日期选择器中读取值
Posted
技术标签:
【中文标题】无法从 bootStrap 日期选择器中读取值【英文标题】:Cannot read value from bootStrap datepicker 【发布时间】:2018-11-21 22:12:35 【问题描述】:我目前正在使用引导日期选择器,但遇到了问题。我想以格式(dd/mm/yyyy)从日期选择器中读取值。但是,即使用户选择了日期值,我仍然会收到一个空字符串。这是我的代码
$('#startDateInput').datepicker(
format: 'dd/mm/yyyy',
);
$('#saveButton').on('click', function ()
var collectedStartDate = $('#startDateInput').datepicker().val();
alert(collectedStartDate);
);
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.js"></script>
<div class="form-group col-md-12">
<label class="control-label col-md-4" for="startDateInput">Start date</label>
<div class="input-group date" id="startDateInput" style="max-width: 280px" data-provide="datepicker">
<input type="text" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</div>
</div>
</div>
<div class="form-group col-md-12">
<label class="control-label col-md-1"></label>
<div class="col-md-10">
<div class="pull-right">
<input type="button" class="btn btn-primary" value="Save" id="saveButton" />
</div>
</div>
</div>
【问题讨论】:
你看过这个帖子吗:***.com/questions/16681875/… How to get the selected date value while using Bootstrap Datepicker?的可能重复 Getting value from JQUERY datepicker的可能重复 【参考方案1】:试试
$('#startDateInput').data('datepicker').getFormattedDate('dd/mm/yyyy')
$('#startDateInput').datepicker(
format: 'dd/mm/yyyy',
);
$('#saveButton').on('click', function()
var collectedStartDate = $('#startDateInput').data('datepicker').getFormattedDate('dd/mm/yyyy');
alert(collectedStartDate);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
<div class="form-group col-md-12">
<label class="control-label col-md-4" for="startDateInput">Start date</label>
<div class="input-group date" id="startDateInput" style="max-width: 280px" data-provide="datepicker">
<input type="text" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</div>
</div>
</div>
<div class="form-group col-md-12">
<label class="control-label col-md-1"></label>
<div class="col-md-10">
<div class="pull-right">
<input type="button" class="btn btn-primary" value="Save" id="saveButton" />
</div>
</div>
</div>
【讨论】:
【参考方案2】:编辑
.datepicker('getDate');
返回 javascript date object
var dateInput = $('#startDateInput').datepicker(
format: 'dd/mm/yyyy',
);
$('#saveButton').on('click', function()
var collectedStartDate = dateInput.datepicker('getDate');
alert(collectedStartDate.getDate() + '/' + (collectedStartDate.getMonth() + 1) + '/' + collectedStartDate.getFullYear());
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
<div class="form-group col-md-12">
<label class="control-label col-md-4" for="startDateInput">Start date</label>
<div class="input-group date" id="startDateInput" style="max-width: 280px" data-provide="datepicker">
<input type="text" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</div>
</div>
</div>
<div class="form-group col-md-12">
<label class="control-label col-md-1"></label>
<div class="col-md-10">
<div class="pull-right">
<input type="button" class="btn btn-primary" value="Save" id="saveButton" />
</div>
</div>
</div>
【讨论】:
您好,如何仅在 dd/mm/yyyy 中获取日期?因为您的解决方案给了我一个更具体的日期,这不是我想要的 Thu Jun 14 2018 00:00:00 GMT+0800(新加坡标准时间)以上是关于无法从 bootStrap 日期选择器中读取值的主要内容,如果未能解决你的问题,请参考以下文章