【问题标题】:Take date of input and enter it as the minDate of the Jquery datepicker获取输入日期并将其输入为 Jquery datepicker 的 minDate
【发布时间】:2021-05-11 10:12:22
【问题描述】:

我有以下输入:

<input type="hidden" class="old_ship_date" name="old_ship_date" value="<?php echo esc_attr( substr( $current_date, 0, 10 ) ); ?>">

该输入中的日期是“yyyy-mm-dd”。

我想将该日期输入到日期选择器的minDate 选项中。有什么方法可以让我输入日期吗?

jQuery(document).ready(function($){
    var now = new Date();
    $('#mypicker').datepicker({
    //options
    dateFormat: "yyyy-mm-dd",
    minDate: $('.old_ship_date').val(),
    firstDay: 0,
    onSelect: function(dateText) {
        $('.new_ship_date').val('');
        $('.new_ship_date').val(dateText);
    },
    beforeShowDay: $.datepicker.noWeekends,
});

【问题讨论】:

    标签: jquery datepicker jquery-ui-datepicker


    【解决方案1】:

    像下面这样试试

    jQuery(document).ready(function($){
      var now = new Date();
    
      var dt =  $('.old_ship_date').val();
      $('#mypicker').datepicker({
        //options
        dateFormat: "yyyy-mm-dd",
        minDate: new Date(dt),
        firstDay: 0,
        onSelect: function(dateText) {
          $('.new_ship_date').val('');
          $('.new_ship_date').val(dateText);
        },
        beforeShowDay: $.datepicker.noWeekends,
      }); 
    

    【讨论】:

      【解决方案2】:

      隐藏输入真的有必要吗?

      然后:

      1. minDate 必须是日期对象。
      2. 为了确保不会出现时区问题,通常会奇怪地减去 1 天... 也有必要指定时间。所以将00:00:00 添加到日期...

      jQuery(document).ready(function($){
        $('#mypicker').datepicker({
          //options
          dateFormat: "yyyy-mm-dd",
          minDate: new Date("<?php echo esc_attr( substr( $current_date, 0, 10 ) ); ?> 00:00:00"),
          firstDay: 0,
          onSelect: function(dateText) {
              $('.new_ship_date').val('');
              $('.new_ship_date').val(dateText);
          },
          beforeShowDay: $.datepicker.noWeekends,
        });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-26
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 2015-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多