在bootstrap datepicker上单击触发器jQuery addClass不起作用

时间:2022-11-30 11:14:22

I'm using bootstrap DatePciker , and I want to add a Click event on days (td), When I click on any day, add class to that day (td). but it does not work.

我正在使用bootstrap DatePciker,我想在days(td)上添加Click事件,当我点击任何一天时,将该类添加到当天(td)。但它不起作用。

$('#calendar').datepicker({
    multidate: true,
    todayHighlight: true,
    todayBtn: true
});

$('tbody').on('click','td.day',function(e){
    e.preventDefault();
    $(this).addClass('clicked');
});

working example on JSFiddle

关于JSFiddle的工作示例

1 个解决方案

#1


You need to use changeDate event of datePicker.

您需要使用datePicker的changeDate事件。

Docs: http://bootstrap-datepicker.readthedocs.org/en/latest/events.html

$('#calendar').datepicker('setDates', events)
    .on('changeDate', function (e) {
        // $(this).addClass('clicked'); // $(this) here is not what you think.
        $('.active').addClass('clicked');
    });

Demo: http://jsfiddle.net/2L1ovhbm/13/

#1


You need to use changeDate event of datePicker.

您需要使用datePicker的changeDate事件。

Docs: http://bootstrap-datepicker.readthedocs.org/en/latest/events.html

$('#calendar').datepicker('setDates', events)
    .on('changeDate', function (e) {
        // $(this).addClass('clicked'); // $(this) here is not what you think.
        $('.active').addClass('clicked');
    });

Demo: http://jsfiddle.net/2L1ovhbm/13/