bootstrap datepicker change date event doesnt fire up when manually editing dates or clearing date

前端 未结 9 2124
离开以前
离开以前 2020-12-09 08:47


        
相关标签:
9条回答
  • 2020-12-09 09:07

    In version 2.1.5

    • bootstrap-datetimepicker.js
    • http://www.eyecon.ro/bootstrap-datepicker
    • Contributions:
    • Andrew Rowls
    • Thiago de Arruda
    • updated for Bootstrap v3 by Jonathan Peterson @Eonasdan

    changeDate has been renamed to change.dp so changedate was not working for me

    $("#datetimepicker").datetimepicker().on('change.dp', function (e) {
                FillDate(new Date());
        });
    

    also needed to change css class from datepicker to datepicker-input

    <div id='datetimepicker' class='datepicker-input input-group controls'>
       <input id='txtStartDate' class='form-control' placeholder='Select datepicker' data-rule-required='true' data-format='MM-DD-YYYY' type='text' />
       <span class='input-group-addon'>
           <span class='icon-calendar' data-time-icon='icon-time' data-date-icon='icon-calendar'></span>
       </span>
    </div>
    

    Date formate also works in capitals like this data-format='MM-DD-YYYY'

    it might be helpful for someone it gave me really hard time :)

    0 讨论(0)
  • 2020-12-09 09:13

    You have to use the change event on the input itself if you want to respond to manual input, because the changeDate event is only for when the date is changed using the datepicker.

    Try something like this:

    $(document).ready(function() {
        $('.datepicker').datepicker({
            format: "yyyy-mm-dd",
        })
        //Listen for the change even on the input
        .change(dateChanged)
        .on('changeDate', dateChanged);
    });
    
    function dateChanged(ev) {
        $(this).datepicker('hide');
        if ($('#startdate').val() != '' && $('#enddate').val() != '') {
            $('#period').text(diffInDays() + ' d.');
        } else {
            $('#period').text("-");
        }
    }
    
    0 讨论(0)
  • 2020-12-09 09:14

    Try this:

    $(".datepicker").on("dp.change", function(e) {
        alert('hey');
    });
    
    0 讨论(0)
  • 2020-12-09 09:14

    I was using AngularJS and AngularStrap 2.3.7 and trying to catch the 'change' event by listening to a <form> element (not the input itself) and none of the answers here worked for me. I tried to do:

    $(form).on('change change.dp dp.change changeDate' function () {...})
    

    And nothing would fire. I ended up listening to the focus and blur events and setting a custom property before/after on the element itself:

    // special hack to make bs-datepickers fire change events
    // use timeout to make sure they all exist first
    $timeout(function () {  
        $('input[bs-datepicker]').on('focus', function (e){
            e.currentTarget.focusValue = e.currentTarget.value;
        });        
        $('input[bs-datepicker]').on('blur', function (e){
            if (e.currentTarget.focusValue !== e.currentTarget.value) {
                var event = new Event('change', { bubbles: true });
                e.currentTarget.dispatchEvent(event);
            }
        });
    })
    

    This basically manually checks the value before and after the focus and blur and dispatches a new 'change' event. The { bubbles: true } bit is what got the form to detect the change. If you have any datepicker elements inside of an ng-if you'll need to wrap the listeners in a $timeout to make sure the digest happens first so all of your datepicker elements exist.

    Hope this helps someone!

    0 讨论(0)
  • 2020-12-09 09:20

    I found a short solution for it. No extra code is needed just trigger the changeDate event. E.g. $('.datepicker').datepicker().trigger('changeDate');

    0 讨论(0)
  • 2020-12-09 09:21

    This should make it work in both cases

    function onDateChange() {
       // Do something here
    }
    
    $('#dpd1').bind('changeDate', onDateChange);
    $('#dpd1').bind('input', onDateChange);
    
    0 讨论(0)
提交回复
热议问题