简体   繁体   English

防止取消选择日期(Bootstrap-datepicker沙箱)

[英]Prevent deselect date (Bootstrap-datepicker sandbox)

I have a question about the bootstrap-datepicker . 我对bootstrap-datepicker有疑问。

When I select a random Date and then open the Popup again and click the same Date, the Input-Field clears. 当我选择一个随机日期,然后再次打开弹出窗口并单击相同的日期时,输入字段将清除。 Is there any way to prevent this from happening? 有什么办法可以防止这种情况的发生?

I need this because in my case the Input-Field is not allowed to be empty. 我需要这样做,因为在我的情况下,不允许输入字段为空。

I use Angular, and have a directive defined that inserts this little snip of hacky javascript... you might be able to do something similar: 我使用Angular,并定义了一条指令,该指令插入了这小小的hacky javascript代码...您可能可以执行类似的操作:

.on('changeDate', function (ev) {
    if (ev.dates && !ev.date && selectedDate)
    {
        // Fixes bug in bootstrap calendar without multiselect, where picking the same day unselects it
        $(this).datepicker('setDate',selectedDate);
    }
    else if (ev.date && ev.date!=selectedDate) selectedDate = ev.date;
});

By having a variable named "selectedDate" somewhere before the constructor for your datepicker, this additional event handler will store valid dates and strip the bogus deselection. 通过在日期选择器的构造函数之前的某个位置放置一个名为“ selectedDate”的变量,此附加事件处理程序将存储有效日期并去除伪造的取消选择。 You'll notice the event object passed includes the array "dates" when incorrectly deselected, and only "date" when correctly selected. 您会注意到传递的事件对象在错误地取消选择时包括数组“ dates”,在正确选择时仅包括“ date”。

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

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