简体   繁体   English

无法使用 jquery ui 的日期选择器设置默认日期

[英]unable to set default date with jquery ui's datepicker

My HTML:我的HTML:

<input type="text" name="day" id="date">

My Javascript:我的Javascript:

$(function() {
    $('#date').datepicker({
        'dateFormat': 'yy-mm-dd',
        'defaultDate': '2015-12-18'
     });
});

The #date text box is being turned into a datepicker, so that's working. #date文本框正在变成一个日期选择器,这样就可以了。 And the format is in the format I'm telling it to be in. But the default date option doesn't seem to be doing anything.格式是我告诉它的格式。但是默认日期选项似乎没有做任何事情。

In this example I'd expect the textbox to appear with "2015-12-18" when the page is first loaded.在此示例中,我希望在首次加载页面时文本框显示为“2015-12-18”。 If I click on the textbox the datepicker dialog will come up and I can enter in a different date but the date of "2015-12-18" should be displaying until I've done that.如果我单击文本框,将出现日期选择器对话框,我可以输入不同的日期,但在我完成之前应该显示“2015-12-18”的日期。 But it isn't and I don't understand why.但事实并非如此,我不明白为什么。

My JS Fiddle:我的 JS 小提琴:

https://jsfiddle.net/18000rLe/4/ https://jsfiddle.net/18000rLe/4/

Try this.试试这个。

Couple of things to keep in mind -->有几件事要记住 -->

1. defaultDate option would set the date in the datepicker calender , not in the input field. 1. defaultDate选项将在日期datepicker calender中设置日期,而不是在input字段中。 For that, you need to explicitly define the setDate .为此,您需要明确定义setDate
2. If you use ' setDate ' in the datepicker , you don't need to add defaultDate as the datepicker would automatically pick the date from the input field. 2.如果您在datepicker中使用' setDate ',则不需要添加defaultDate ,因为datepicker会自动从input字段中选择日期。

  $(function() {
        $("#date").datepicker({dateFormat: 'yy-mm-dd'});
        $("#date").datepicker('setDate', new Date('2014-12-18')); 
    }); 

Example: http://jsfiddle.net/DinoMyte/tXyLn/527/示例:http: //jsfiddle.net/DinoMyte/tXyLn/527/

you could use this it will give you the date:)你可以用这个它会给你日期:)

 $(function() { var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; //January is 0. var yyyy = today;getFullYear(); if(dd < 10) { dd = '0' + dd; } if (mm < 10) { mm = '0' + mm; } today = yyyy + '-' + mm + '-' + dd. $('#date');val(today); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="day" id="date">

You can apply it like this:你可以像这样应用它:

$(function() {
    $('#date').datepicker({
        'dateFormat': 'yy-mm-dd',
      //'defaultDate': +7
      //'defaultDate': new Date(1985, 00, 01),
      'defaultDate': '2015-12-18'
     }).datepicker("setDate",new Date());
});

I had to pass in date this way instead of Date object:我必须以这种方式传递日期而不是Date对象:

$('#date input').datepicker({
  'defaultDate': {
    year: 1999,
    month: 11,
    day: 31
 });

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

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