jQuery datepicker默认日期和多个datepickers

问题描述 投票:-1回答:1

我正在使用此日期选择器https://jqueryui.com/datepicker/

我有2个日期字段,格式相同。我需要第一个字段作为默认日期今天加载。第二个是今天的日期-7(1周前。)

出于某种原因,无法使它正常工作。我可以使两个字段都正常工作,但它们不会加载任何默认日期。

感谢您的任何帮助。


 $(function() {   
       $( "#from" ).datepicker({   
      defaultDate: "+1w",  
      changeMonth: true,   
      numberOfMonths: 1,  
      onClose: function( selectedDate ) {  
        $( "#to" ).datepicker( "option", "minDate", selectedDate );  
      }  
    });  
    $( "#to" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 1,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });  
  });  

Start date: <input type="text" id="from" name="from"> 
End date: <input type="text" id="to" name = "to"> 
jquery date jquery-ui datepicker jquery-ui-datepicker
1个回答
0
投票

您将需要使用setDate方法。查看更多:https://api.jqueryui.com/datepicker/#method-setDate

设置日期选择器的日期。新日期可以是Date对象或当前日期格式的字符串(例如"01/26/2009"),从今天起的天数(例如+7)或值和句点的字符串("y" "m"代表月份,"w"代表星期,"d"代表几天,例如"+1m +7d"

$(function() {
  // Initialize Date Fields
  var from = $("#from").datepicker({
    changeMonth: true,
    numberOfMonths: 1,
    onClose: function(selectedDate) {
      $("#to").datepicker("option", "minDate", selectedDate);
    }
  });
  var to = $("#to").datepicker({
    changeMonth: true,
    numberOfMonths: 1,
    onClose: function(selectedDate) {
      $("#from").datepicker("option", "maxDate", selectedDate);
    }
  });
  // Set Dates
  from.datepicker("setDate", "+0d");
  to.datepicker("setDate", "-7d");
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<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>

Start date: <input type="text" id="from" name="from"> End date: <input type="text" id="to" name="to">
© www.soinside.com 2019 - 2024. All rights reserved.