Javascript 使用空值初始化 JQuery Datepicker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11228931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:00:32  来源:igfitidea点击:

Initialize JQuery Datepicker with a blank value

javascriptjquery

提问by Doug Chamberlain

This code still sets the field to today's date

此代码仍将字段设置为今天的日期

<script type="text/javascript">
    $(document).ready(function () {
        $('.date').datepicker({ dateFormat: "mm/dd/yy", changeMonth: true,
            changeYear: true, yearRange: '1900:2020', defaultDate: ''
        });

    });

</script>

How do I universally configure the field to start as blank?

我如何普遍配置该字段以开始为空白?

回答by dxh

If the field is showing today's date it's because of how the input field is populated. The datepicker will start with whatever value the html element has, regardless of what that is:

如果该字段显示今天的日期,那是因为输入字段的填充方式。日期选择器将从 html 元素具有的任何值开始,无论它是什么:

Demo

演示

If you cannot control the value of the input field through changing markup, but still needs the input field to be reset, you'll have to do this manually:

如果您无法通过更改标记来控制输入字段的值,但仍需要重置输入字段,则必须手动执行此操作:

$(document).ready(function () {
    $('.date').datepicker({ dateFormat: "mm/dd/yy", changeMonth: true,
        changeYear: true, yearRange: '1900:2020'
    }).val('');
});