在 Jquery 中设置最小日期

Set minDate in Jquery

我想知道如何设置minDate。我用这个

$(function () {
    $("#dph-entry").datepicker('option', 'minDate', new Date(2017, 3, 27));
});

输入

<input type='text' class="con-input" id='dph-entry' placeholder="Entrada"/>

但不起作用

您需要先初始化datepicker()。那么只能设置option的。

$("#dph-entry").datepicker();
$("#dph-entry").datepicker('option', 'minDate', new Date(2017, 3, 27));

初始化时更好地传递选项

$("#dph-entry").datepicker({'minDate': new Date(2017, 3, 27)});

$(function() {
  $("#dph-entry").datepicker({
    'minDate': new Date(2017, 3, 27)
  });
});
<link href="https://code.jquery.com/ui/jquery-ui-git.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<input type='text' class="con-input" id='dph-entry' placeholder="Entrada" />

你使用它的方式就是你如何在初始化后设置最小日期。

Get or set the minDate option, after initialization:

// Getter

var minDate = $( ".selector" ).datepicker( "option", "minDate" );

// Setter

$( ".selector" ).datepicker( "option","minDate", new Date(2007, 1 - 1, 1) );

当您尝试设置默认的最小日期时,这意味着您正在尝试使用最小日期初始化日期选择器,这就是为什么您需要添加一些不同的选项,property: value:

Initialize the datepicker with the minDate option specified:

$( ".selector" ).datepicker({ minDate: new Date(2007, 1 - 1, 1) });

请看下面的工作片段:

$(function() {
  $(document).ready(function() {
    //$("#dp-entry").datepicker('option', 'minDate', new Date(2017, 3, 27));
    $("#dph-entry").datepicker({minDate: new Date(2017, 3, 27)});
  })
});
<link href="https://code.jquery.com/ui/jquery-ui-git.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<input type='text' class="con-input" id='dph-entry' placeholder="Entrada" />

你可以看看我说的here.