简体   繁体   中英

How to call “onSelect” function for bootstrap3-datetimepicker? jQuery

I am using Rails 4 and I have the gem bootstrap3-datetimepicker-rails installed.

It is working fine, but I want to be able to do an onSelect call like I can with the normal bootstrap-datepicker.

I want to be able to do something like this:

$("#start").datetimepicker({
    minDate: new Date()
}).on("change", function(selectedDate) {
    $("#end").datetimepicker({ minDate: selectedDate });
});

$("#end").datetimepicker();

but the documentation is very vague and doesn't seem to include anything on this concept. I am still pretty surprised that this feature of validating start and end dates is not a native option in any date or time picker.

How would I realize this?

Note : I am using bootstrap3-date time picker, not the regular bootstrap-datepicker and not jquery-datetimepicker.

Side Note: I've always felt that the world's most ideal automatic datepicker validation would look something like this:

$("#start").datepicker({
    minDate: new Date(),
    endDate: "#end"  // Would automatically call $("#end").datepicker and set the minDate to whatever #start's selected date is
});

I am considering creating my own branch to try and implement it because I feel that the concept of start and end dates is commonly ignored when datepickers are created but is so frequently needed.

Take a look at the documentation section on linked pickers: https://eonasdan.github.io/bootstrap-datetimepicker/#linked-pickers

In your case you would do:

$("#start").on("dp.change", function (e) {
    $('#end').data("DateTimePicker").minDate(e.date);
});

Try this,

$("#start").datetimepicker({
    minDate: new Date()
}).on("change.dp", function(selectedDate) {
    $("#end").datetimepicker({ minDate: selectedDate });
});

If not, try this,

$("#start").datetimepicker({
        minDate: new Date()
    }).on("dp.change", function(selectedDate) {
        $("#end").datetimepicker({ minDate: selectedDate });
    });

Check bootstrap-3-datetimepicker-events-not-firing-up for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM