手动编辑日期或清除日期时,引导程序日期选择器更改日期事件不会触发 [英] bootstrap datepicker change date event doesnt fire up when manually editing dates or clearing date

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

问题描述

<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>

这就是我的日期选择器的样子.所以基本上当我通过单击鼠标更改日期时效果很好,但是当我用键盘手动更改日期或手动清除日期更改日期事件时不会被调用.这是一个错误还是我在这里做错了什么?

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 ?

推荐答案

如果你想响应手动输入,你必须在输入本身上使用 change 事件,因为 changeDate 事件仅适用于使用日期选择器更改日期时.

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

这篇关于手动编辑日期或清除日期时,引导程序日期选择器更改日期事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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