启动日期拾取器,getDate不工作

Bootstrap-datepicker, getDate not working

本文关键字:getDate 工作 日期 启动      更新时间:2023-09-26

我想弄清楚如何才能获得用户在日期选择器上选择的日期。我已经尝试了这个网站上提到的许多东西,但它似乎不起作用。下面是我的代码:

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

当我不运行。click(…)时,日期选择器看起来很好,所以我相信问题是存在的。

谢谢

右括号放错地方了,处理程序需要是函数,像这样:

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

开始和结束日期的标记如下:

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

初始化日期选择器的代码如下:

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

当我试图通过'#publishedDateSelector').datepicker('getDate')获得日期时,它曾经返回一个不是日期表示的jQuery对象。我得到日期的方法是:

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