简体   繁体   English

Bootstrap-datepicker,getDate不起作用

[英]Bootstrap-datepicker, getDate not working

I'm trying to figure out how I can get the date the user has selected on the datepicker. 我试图弄清楚如何获取用户在日期选择器上选择的日期。 I've tried many things mentionned on this site, but it doesn't seem to work. 我已经尝试了此站点上提到的许多内容,但似乎没有用。 Here's my code: 这是我的代码:

            // script for the date picker inline
            $('#calendar1 div').datepicker({
                keyboardNavigation: false,
                todayHighlight: true

                $("#calendar1 div").click( 
                var date = $("#calendar1 div").datepicker("getDate");
            });
            });

            // script for the datepicker text input
            $('#calendar2 .input-group.date').datepicker({
                keyboardNavigation: false,
                todayHighlight: true

                $("#calendar2 .input-group.date").click( 
                var date = $("#calendar2 .input-group.date").datepicker("getDate");
            });
            });

The datepickers appear fine when I don't run the .click(...), so I believe the problem is there. 当我不运行.click(...)时,日期选择器似乎很好,所以我相信问题就在那里。

Thanks 谢谢

You have your closing braces in the wrong place, and your handlers need to be functions, like this: 您将右花括号放在错误的位置,并且处理程序需要为函数,如下所示:

// script for the date picker inline
$('#calendar1 div').datepicker({
    keyboardNavigation: false,
    todayHighlight: true
});

$("#calendar1 div").click(function() {
    // this is the selected element
    var date = $(this).datepicker("getDate");
});

// script for the datepicker text input
$('#calendar2 .input-group.date').datepicker({
    keyboardNavigation: false,
    todayHighlight: true
});

$("#calendar2 .input-group.date").click(function() {
    // this is the selected element
    var date = $(this).datepicker("getDate");
});

I had my markup like below for start and end date: 我的开始和结束日期如下所示:

<div class="input-daterange dateselector"
                            id="publishedDateSelector">
    <input type="text" class="input-small form-control" /><input
                                type="text" class="input-small form-control" />
</div>

The following code initializing the datepicker: 以下代码初始化日期选择器:

$('.dateselector').datepicker({
    clearBtn : true,
    orientation : "top",
    todayHighlight : true
});

When I tried to get the date by '#publishedDateSelector').datepicker('getDate') , it used to return a jQuery object which was not a date representation. 当我尝试通过'#publishedDateSelector').datepicker('getDate')获取日期时,它用来返回不是日期表示形式的jQuery对象。 The way I got the dates are by: 我约会的方式是:

'#publishedDateSelector input:first').datepicker('getDate'); //StartDate
'#publishedDateSelector input:last').datepicker('getDate');  //EndDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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