我目前正在使用bootstrap datepicker,我遇到了问题。我想以格式(dd / mm / yyyy)读取datepicker中的值。但是,即使用户选择了日期值,我仍然会收到一个空字符串。这是我的代码
$('#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>
答案 0 :(得分:1)
修改强>
.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>
答案 1 :(得分: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>