jQuery datepicker默认日期和多个datepicker

我正在使用此日期选择器 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",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"> 
naomibest 回答:jQuery datepicker默认日期和多个datepicker

您将需要使用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,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">

本文链接:https://www.f2er.com/2905682.html

大家都在问