防止显示事件在日期选择器上触发

prevent show event from firing when on datepicker

我有一个 bootstrap 日期选择器。我希望在首次单击输入框时触发 show 事件。但是,如果用户使用上一个和下一个箭头遍历月份,我不希望触发该事件。

这可能吗?

$('#sandbox-container input').datepicker({
    autoclose: true
});

$('#sandbox-container input').on('show', function(e){
    console.log('show event');
});

如果您 运行 fiddle 您将在第一次单击输入字段时看到消息被记录(我想要这种行为)。但是,如果您使用上一个和下一个箭头,您也会看到显示该消息。

JS Fiddler 来了 - http://jsfiddle.net/LcqM7/611/

您将需要检查当前 "state" 中的 show 是否已经 "run"。

$('#sandbox-container input')
    .on('show', function(e){
        if (! $(this).data('showrun')) {
            console.log('show event');
            $(this).data('showrun', true);
        }
    })
    .on('hide', function(e){
      $(this).data('showrun', false);
    })
    .on('changeMonth', function(e){
        console.log('change month event');
    });

还有一个工作示例:
http://jsfiddle.net/k6b3yepb/

这有点严峻,但您可以利用 hide 事件解除 show 的绑定,然后使用 jqueries one.

重新附加它

http://jsfiddle.net/5dhkumko/

$('#sandbox-container input').datepicker({
    autoclose: true
});

$('#sandbox-container input').one('show', showMethod);

$('#sandbox-container input').on('hide', function(e) {
    $('#sandbox-container input').off('show', showMethod).one('show', showMethod);
});

function showMethod() {
    console.log('show event');
}