I have an input element with an attached datepicker created using bootstrap-datepicker.
-
All others answers are related to jQuery UI datepicker
, but the question is about bootstrap-datepicker
.
You can use the on changeDate event to trigger a change event on the related input, and then handle both ways of changing the date from the onChange
handler:
changeDate
Fired when the date is changed.
Example:
$('#dp3').datepicker().on('changeDate', function (ev) {
$('#date-daily').change();
});
$('#date-daily').val('0000-00-00');
$('#date-daily').change(function () {
console.log($('#date-daily').val());
});
Here is a working fiddle: http://jsfiddle.net/IrvinDominin/frjhgpn8/
讨论(0)
-
Based on Irvin Dominin example, I've created 2 examples supporting Paste and hit Enter.
This works in Chrome:
http://jsfiddle.net/lhernand/0a8woLev/
$(document).ready(function() {
$('#date-daily').datepicker({
format: 'dd/mm/yyyy',
assumeNearbyYear: true,
autoclose: true,
orientation: 'bottom right',
todayHighlight: true,
keyboardNavigation: false
})
/* On 'paste' -> loses focus, hide calendar and trigger 'change' */
.on('paste', function(e) {
$(this).blur();
$('#date-daily').datepicker('hide');
})
/* On 'enter' keypress -> loses focus and trigger 'change' */
.on('keydown', function(e) {
if (e.which === 13) {
console.log('enter');
$(this).blur();
}
})
.change(function(e) {
console.log('change');
$('#stdout').append($('#date-daily').val() + ' change\n');
});
});
But not in IE, so I created another example for IE11:
https://jsbin.com/timarum/14/edit?html,js,console,output
$(document).ready(function() {
$('#date-daily').datepicker({
format: 'dd/mm/yyyy',
assumeNearbyYear: true,
autoclose: true,
orientation: 'bottom right',
todayHighlight: true,
keyboardNavigation: false
})
// OnEnter -> lose focus
.on('keydown', function(e) {
if (e.which === 13){
$(this).blur();
}
})
// onPaste -> hide and lose focus
.on('keyup', function(e) {
if (e.which === 86){
$(this).blur();
$(this).datepicker('hide');
}
})
.change(function(e) {
$('#stdout').append($('#date-daily').val() + ' change\n');
});
});
If last example still doesn't work in IE11, you can try splitting the setup:
// DatePicker setup
$('.datepicker').datepicker({
format: 'dd/mm/yyyy',
assumeNearbyYear: true, /* manually-entered dates with two-digit years, such as '5/1/15', will be parsed as '2015', not '15' */
autoclose: true, /* close the datepicker immediately when a date is selected */
orientation: 'bottom rigth',
todayHighlight: true, /* today appears with a blue box */
keyboardNavigation: false /* select date only onClick. when true, is too difficult free typing */
});
And the event handlers: (note I'm not using $('.datepicker').datepicker({
)
// Smoker DataPicker behaviour
$('#inputStoppedDate')
// OnEnter -> lose focus
.on('keydown', function (e) {
if (e.which === 13){
$(this).blur();
}
})
// onPaste -> hide and lose focus
.on('keyup', function (e) {
if (e.which === 86){
$(this).blur();
$(this).datepicker('hide');
}
})
.change(function (e) {
// do saomething
});
讨论(0)
-
$(document).ready(function(){
$("#dateFrom").datepicker({
todayBtn: 1,
autoclose: true,
}).on('changeDate', function (selected) {
var minDate = new Date(selected.date.valueOf());
$('#dateTo').datepicker('setStartDate', minDate);
});
$("#dateTo").datepicker({
todayBtn: 1,
autoclose: true,}) ;
});
讨论(0)
-
$(function() {
$('input[name="datetimepicker"]').datetimepicker({
defaultDate: new Date()
}).on('dp.change',function(event){
$('#newDateSpan').html("New Date: " + event.date.format('lll'));
$('#oldDateSpan').html("Old Date: " + event.oldDate.format('lll'));
});
});
<link href="http://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
<script src="http://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>
<div class="container">
<div class="col-xs-12">
<input name="datetimepicker" />
<p><span id="newDateSpan"></span><br><span id="oldDateSpan"></span></p>
</div>
</div>
讨论(0)
-
Try this:
$("#dp3").on("dp.change", function(e) {
alert('hey');
});
讨论(0)
-
Here is my code for that:
$('#date-daily').datepicker().on('changeDate', function(e) {
//$('#other-input').val(e.format(0,"dd/mm/yyyy"));
//alert(e.date);
//alert(e.format(0,"dd/mm/yyyy"));
//console.log(e.date);
});
Just uncomment the one you prefer.
The first option changes the value of other input element.
The second one alerts the date with datepicker default format.
The third one alerts the date with your own custom format.
The last option outputs to log (default format date).
It's your choice to use the e.date , e.dates (for múltiple date input) or e.format(...).
here some more info
讨论(0)