简体   繁体   English

设置日期选择器输入字段的初始值

[英]Set initial value of datepicker input field

I'm using the jQuery UI datepicker 我正在使用jQuery UI datepicker

I need to put the start date automatically in the input type text. 我需要将开始日期自动放入输入类型的文本中。

$(function() {
  $("#datepicker").datepicker({
    dateFormat: 'DD, d MM, yy'
  }).bind("change", function() {
    var minValue = $(this).val();
    minValue = $.datepicker.parseDate("DD, d MM, yy", minValue);
    minValue.setDate(minValue.getDate());
    $("#datepicker").datepicker("option", "minDate", minValue);
  })
});

You can set a variable containing your start date, in my case it's the current date. 您可以设置一个包含开始日期的变量,在我的情况下,它是当前日期。

The set the minDate (or defaultDate ) of the datepicker upon initialization. 在初始化时设置日期选择器的minDate (或defaultDate )。 Then you can set the value of the input using your start date. 然后,您可以使用开始日期设置input的值。

 $(function() { var startDate = new Date(); $("#datepicker").datepicker({ dateFormat: 'DD, d MM, yy', minDate: startDate }).bind("change", function() { var minValue = $(this).val(); minValue = $.datepicker.parseDate("DD, d MM, yy", minValue); minValue.setDate(minValue.getDate()); $("#datepicker").datepicker("option", "minDate", minValue); }).val($.datepicker.formatDate("DD, d MM, yy", startDate)); }); 
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" <p>Date: <input type="text" id="datepicker"> </p> 

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

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