简体   繁体   English

引导日期选择器设置默认日期不起作用

[英]bootstrap datepicker set default date not working

I am using bootstrap datepicker.I want to show 01-01-1960 as selected date in my calendar when it is loaded first time. 我正在使用引导日期选择器,我想在第一次加载日历时在日历中显示01-01-1960作为选定日期。

javascript javascript

 <script src="~/Content/js/datepicker/bootstrap-datepicker.js"></script>
 <link href="~/Content/js/datepicker/datepicker.css" rel="stylesheet" />

 <script type="text/javascript">
$(document).ready(function () {
      $('.datepicker-input').datepicker({
        defaultViewDate: { year: 1960, month: 01, day: 01 },
        format: 'mm-dd-yyyy',
    });

});

I am using this theme - http://flatfull.com/themes/scale/form-elements.html . 我正在使用这个主题-http://flatfull.com/themes/scale/form-elements.html

You can set default date like : 您可以设置默认日期,例如:

 <script src="~/Content/js/datepicker/bootstrap-datepicker.js"></script>
 <link href="~/Content/js/datepicker/datepicker.css" rel="stylesheet" />

 <script type="text/javascript">
$(document).ready(function () {
      $('.datepicker-input').datepicker({
        'setDate', new Date(1960, 01, 01 ),
        'format': 'mm-dd-yyyy',
    });

});
</script>

Try with this. 试试这个。

<script type="text/javascript">    
$(document).ready(function () {

        $('.datepicker-input').datepicker({
        dateFormat: 'yy-mm-dd'
    });
          $('.datepicker-input').datepicker('setDate', new Date(1960,00,01));

          $('.datepicker-input').val('');
    });
</script>

It Works for me. 这个对我有用。

$('.datepicker-input').datepicker({
    useCurrent: false,
    setDate: new Date(1960, 01, 01),
});

Set useCurrent to false , then setDate would work. useCurrent设置为false ,则setDate将起作用。

To fix the following error: 要修复以下错误:

error-Uncaught TypeError: data[option] is not a function 错误未捕获的TypeError:data [option]不是函数

You should use the option setValue instead of setDate! 您应该使用选项setValue而不是setDate!

$(".datepicker-input").datepicker("setValue", new Date());

I found the simple solution for setting default value on this problem. 我找到了在此问题上设置默认值的简单解决方案。

According to the research from google, you cannot simple add 'default date' option here but you can set 'setDate' option. 根据Google的研究,您不能在此处简单地添加“默认日期”选项,但可以设置“ setDate”选项。 Thus, For example... 因此,例如...

<div class="fullDate input-group date" id="atdcRegReqDtDiv">
<input type="text" class="form-control inputstl" id="REQ_DT">
<span class="input-group-addon"><i class="dtPick glyphicon glyphicon-calendar"></i></span>
</div>

Add the setDate option, to the object, and then put empty value to the text input. 将setDate选项添加到对象,然后将空值添加到文本输入中。

$('#atdcRegReqDtDiv').datepicker('setDate', "2018-09-14");
$('#REQ_DT').val("");

Then, you can see the empty input box with the calendar has default Year/Month. 然后,您可以看到带有日历的空输入框具有默认的“年/月”。

Good Luck. 祝好运。

您可以使用Jquery defaultDate设置默认日期:

defaultDate: new Date(1960, 01, 01);

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

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