bootstrap-datepicker.js using today's date when date cleared

馋奶兔 提交于 2019-12-11 04:53:43

问题


Using bootstrap-datepicker.js, very nice plugin. Everything works as expected, minus this one case, which could be me missing something within the documentation. Whenever a user enters a date and the textbox looses focus (user goes on to complete the rest of the form), if the user goes back to the textbox and attempts to remove the date, today's date is automatically populated. If you remove today's date, it automatically populates again...repeatedly. Is there an option I have missed that would rectify this situation? If the field was required, it wouldn't be a big deal, however the user has the option of submitting the form without a date. Code below:

       $('#start_date').datepicker({
            orientation: "bottom auto",
            autoclose: true,
            todayHighlight: true,
            useCurrent: false
        }).mask("99/99/9999");

        $('#end_date').datepicker({
            orientation: "bottom auto",
            autoclose: true,
            todayHighlight: true,
            useCurrent: false
        }).mask("99/99/9999");

This is a working fiddle demonstrating the issue: https://jsfiddle.net/f8v5vLmt/3/


回答1:


There must be a problem with the mask plugin you are using. It is setting a value in the field that datetime picker is trying to parse, and it is obviously not a date, so it must be defaulting to current date.

Try adding forceParse: false to the options.

You can check the behaviour is whatyou want in the first field here:

https://jsfiddle.net/f8v5vLmt/5/

Ref: https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#forceparse



来源:https://stackoverflow.com/questions/45828449/bootstrap-datepicker-js-using-todays-date-when-date-cleared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!