twitter-bootstrap jQuery - Bootstrap Datepicker onChange 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19697325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 19:03:18  来源:igfitidea点击:

jQuery - Bootstrap Datepicker onChange issue

javascriptjquerytwitter-bootstrap

提问by John Guan

I am trying change link when a date is selected but whenever I select a date, I get ?day=Here's my code:

我在选择日期时尝试更改链接,但每当我选择日期时,我都会收到 ?day=以下代码:

Markup:

标记:

<i class="icon-calendar"></i>

<i class="icon-calendar"></i>

It only works when I use input <input type="text" class="datepicker">

它仅在我使用输入时有效 <input type="text" class="datepicker">

Javascript:

Javascript:

        $(document).ready(function(){
            $(".datepicker").datepicker({
                format: "yyyy-mm-dd",
                endDate: "-2d"
            })

            .on('changeDate', function(ev){
                var dateData = $('.datepicker').val();
                window.location.href = "?day=" + dateData ;
            });
        });

Expected results: ?day=2013-11-01

预期成绩: ?day=2013-11-01

Thanks

谢谢

回答by koala_dev

According to the documentationyou can get the formatted date with the format()function accessible through the event object:

根据documentation您可以使用format()可通过事件对象访问的函数获取格式化日期:

$(document).ready(function(){
        $(".datepicker").datepicker({
            format: "yyyy-mm-dd",
            endDate: "-2d"
        })
        .on('changeDate', function(ev){
            window.location.href = "?day=" + ev.format();
        });
    });

回答by ethorn10

I'm pretty sure you can access the date via ev:

我很确定您可以通过ev以下方式访问日期:

$(document).ready(function(){
    $(".datepicker").datepicker({
        format: "yyyy-mm-dd",
        endDate: "-2d"
    })

    .on('changeDate', function(ev){
        var dateData = new Date(ev.date);  // this is the change
        window.location.href = "?day=" + dateData ;
    });
});

回答by Michael Villeneuve

I have not tested it but I think it should be more like

我还没有测试过,但我认为它应该更像

$(".datepicker").on('change', function(ev){
    var dateData = $(this).val();
    window.location.href = "?day=" + dateData ;
});