bootstrap datepicker更改日期事件在手动编辑日期或清除日期时不会启动 [英] bootstrap datepicker change date event doesnt fire up when manually editing dates or clearing date

查看:1680
本文介绍了bootstrap datepicker更改日期事件在手动编辑日期或清除日期时不会启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
    // When the document is ready
    $(document).ready(function () {
        $('.datepicker').datepicker({
            format: "yyyy-mm-dd",
        }).on('changeDate', function(ev){
            // do what you want here
            $(this).datepicker('hide');
        }).on('changeDate', function(ev){
                if ($('#startdate').val() != '' && $('#enddate').val() != ''){

                    $('#period').text(diffInDays() + ' d.'); 
                } else {
                    $('#period').text("-");
                }
            });
</script>

所以这里是我的datepicker的样子,所以基本上当我通过点击鼠标改变日期是很好的,但是当我用键盘手动更改日期或手动清除日期更改日期事件没有被调用,这是一个错误,或者我做错了什么ere?

So here's what my datepicker looks like. So basically when I change date by clicking mouse it works good, however when I manually change date with keyboard or manually clear the date change date event doesn't get called. Is this a bug or am I doing something wrong here ?

推荐答案

我猜想你必须使用更改事件在输入本身,如果你想回应手动输入,因为 changeDate 事件只适用于使用datepicker更改日期。

I would guess that 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.

尝试这样的东西:

$(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("-");
    }
}

这篇关于bootstrap datepicker更改日期事件在手动编辑日期或清除日期时不会启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆